Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help!

Started by Gameman17 Nov 28, 2001 at 9:21 PM 5 replies 300+ views
Original Post
Gameman17
Gameman17
I am using this code. code starts////////////////// #include void main() { char name[50]; cout<<"Please enter your name: "; cin.getline(name, 49); cout<<"\n\nHello, "<
Colin Jeanne
Colin Jeanne
You can''t compare char arrays by just writing if (string1 == "bla bla bla"). String functions are required. strcmp() is what you''re looking for.

Invader X
Invader''s Realm
Oluseyi
Oluseyi
I can explain the C way to do what you''re trying to do (which is essentially the way you''ve been going so far), or I can make your life easier using (drum roll)... "The C++ Way!"

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string name;
cout << "What is your name?";
getline(cin, name);
cout << endl << endl << "Hello, " << name << endl;
return 0;
}

If you want to compare the input (to quit for example)

...
if(name == "quit") // note double quotes
{
return 0;
}

Have fun. You can look up string, cout/cin/getline, keyword using, namespaces and the Standard Template Library (STL) in your language/compiler documentation/reference.


Start Here!
Gameman17
Gameman17
I tried What you sudjested Oluseyi and it works but, When I run the program it prints:

///////////////////////////
What is Your name: John doe
_

\\\\\\\\\\\\

then it waits for a second input, it dosn''t print the second one it just prints:

////////////////
hello, John doe
////////////

??????????????????????
Spiral
Spiral
what second input? I think you want your code to keep asking for your name then print it out repeatedly. If the name is "quit" then you want it to stop. Try with a while loop:

        

#include <iostream>

#include <string>

using namespace std;

int main(int argc, char *argv[]){
string name;

while(1) {
cout << "What is your name? " << flush;
cin >> name;
if (name == "quit") break;
cout << endl << "Hello, " << name << endl;
}

return 0;
}



Of course, thats if i understood your post correctly...

Edited by - Spiral on November 29, 2001 9:45:13 AM
--------------------Go Stick Your Head In A Pig
BASSOFeeSH
BASSOFeeSH
What he means by "second input" is that you have to hit twice before the program continues.

I have currently having the same issue b/c I am looking at STL for the first time.

  
#include <iostream>
#include <string>
using namespace std;

int main()
{

string str;


cout << "Input your name: ";

getline(cin, str);

cout << endl << "Your name is " << str;

cout << endl;



return 0;
}


^^^^^^^^^^^^^^^^^^^^^^^^^
Wait, wait, wait....who's Nambla Fett?

Edited by - bassofeesh on December 7, 2001 11:38:33 PM
-- What would Sweetness do?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.