ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm just started to learn C++ from a tutorial on a website while I wait for my two C++ books that i'm getting for the holidays.
My question is: how do you use character variables in an "if" statement in which if a word is inputed that is the same as a certain word in the if statement, it continues with the rest of if statement to output what you want to be said if the two words are the same. To clarify..Here's an example of how i thought it would work:
char name [20];
cout << "What is your name? ";
cin >> name;
if (name == "brad") {
cout << "Hi, Brad";
}
else {
cout << "sorry, I do not know you.";
imports the string.h header file, which allows us to use the new variable type, string.
Code:
using namespace std;
tells the program that we are using the std namespace, so instead of typing std::cout and std::cin, etc, we can simply just type cout and cin, etc.
You can still just use int main(). The one I posted is the fancy smancy one which is used by default in KDevelop.
Code:
string name = "";
Declares the variable name to be of the data type string, which we can now use because we imported the string header file. The rest of the line is initializing the variable to an empty string.
what if i wanted to repeat a char variable that has a space in it
Code:
char name [30];
cout << "What's your full name? ";
cin >> name; //input would be something like Brad Smith
cout << name << ", that's a nice name";
when i try to use spaces the second word just goes to the next cin << variable;
also,
in simpler C++ programs i've made, i didn't have to use the "using namespace std;" for using cout and cin but it seems in the one with the string header file that i do have to use it. How come?
#include <iostream>
using namespace std;
int main()
{
char name [30];
cout << "What's your full name? ";
cin.getline(name, 30); //input would be something like Brad Smith
cout << name << ", that's a nice name";
return EXIT_SUCCESS;
}
The new part here is the use of the getline method of cin. The variable is passed in as the first parameter, and the size is passed in as the second parameter.
As far as your second question goes, regarding
Code:
using namespace std;
I can't tell you why for some of your simpler programs didn't require it, but I would tend to guess they just assumed it since it is so common.
And, for your learning pleasure, a combination of both of your questions, overly commented so that it will hopefully be easy for anyone to understand:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char name [30]; // Declare variable which will allow us to take input
// from user easily
string name2 = ""; // Declare a string variable which will allow us to
// easily compare the value
cout << "What's your full name? ";
cin.getline(name, 30); // Take the current line, up to 30 characters,
// and assign it to the character array name
name2 = name; // Set the value of the string name2 to that of the
// characer array name
cout << name2 << ", that's a nice name.\n";
if (name2 == "Brad Smith") // Make the comparison of string name2 to
{ // "Brad Smith"
cout << "Hey, I think I know you!\n";
}
else
{
cout << "I don't think I've met you before.\n";
}
return EXIT_SUCCESS;
}
And for comparison purposes, the same code (uncommented) written in Python:
Code:
name = raw_input("What is your full name? ")
print name+", that's a nice name."
if name == "Brad Smith":
print "Hey, I think I know you!"
else:
print "I don't think I've met you before."
Last edited by Dodgeram01; 12-12-2004 at 09:13 PM.
Thanks for being such a big help. I was experimenting with cin.getline in my program and on the second one it asks, it seems to skip the input and procedes to output the next 'cout'. i'd rather not post all the code but i'm guessing something in there is screwing it up.
As fas as I know, cin and cout is buffered. That is, cin reads until the first space is 'read' and takes it as the input. The 'rest' of what was typed is still in the buffer. If there is another cin what is in the buffer is 'read' staright in. So it looks as if the prog 'skipped' the cin and went to the cout.
You can use cin.ignore(). This ignores the next char in the buffer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.