User | Post |
Sokarhacd
Posts: 551/1757 |
ah yes, fgets is better, and its actually fgets(name,90,stdin);
thanks though. |
Dish
Posts: 172/596 |
ah yeah... good call about the fgets.
*Disch grumbles at command-prompt programming |
Euclid
Posts: 119/193 |
proper way to use cin/cout in your case, c++ style.
#include < iostream > #include < string > (don't forget these, else that string name won't work)
// declaration string name; ... cout << "Hello, what is your full name?"; cin >> name; cout << "Ah, so your name is " << name << endl;
a << can't be on the same line as >> since they're returning 2 different streams (input/output).
*back to C*
why use gets and risk entering more than 90 chars in this case? use fgets(stdin, name, 90) instead (i think i got them the right way around) |
Sokarhacd
Posts: 550/1757 |
yeah, actually just figured it out...when like 2 minutes ago, and i edited right before you replied lol...well I guess thats solved lol... |
Dish
Posts: 170/596 |
hmm... just tried it and got the same deal.
gets() might be what you want for this... I just tried:
char name[90]; printf("Input name:\n"); gets(name); printf("%s",name);
it worked as expected, even with spaces.
edit:
haha... apparently you already figured it out ;D |
Sokarhacd
Posts: 549/1757 |
I can switch to printf and scanf, I just saw cin and cout, so I tried those, I also just tried printf and scanf and it gives me the exact same thing, and I make it a char name[90]; and it still only gave me everything before a space was entered.
edit: ah ha.....I needed to use gets... apparently....
gets(name); worked perfectly..... |
Dish
Posts: 169/596 |
I can't really get into specifics because I find cout/cin to be horrendously stupid (look into printf() and scanf() for better alternatives unless you absolutly have to use cout/cin like for your school or something). Your 'name' variable should probably be a character array... not a single character nor a 'string' type:
char name[80]; // will hold up to 79 characters (plus null termination)
I would assume cin would give you the entire string typed by the user... but can't say for sure... I dodged cout/cin like the plague when I was learning. |
Sokarhacd
Posts: 548/1757 |
ok...one thing I dont get...ive tried to store a sentence into a variable...ive tried name is the variable char name; string name; and then I do this
cout << "Hello, what is your full name"<cin >>name; cout << "Ah, so your name is "< ....its probably something stupid.
with that, I only get whats typed in before entering a space, like "Hello there", it spits out "Hello" and nothing else...can someone help....
|