|
| takumi05 (18) | |
| I have a loop which outputs the ASCII values but how do I write another loop which adds those values together? string name; int a; cout<<"Enter string : "; cin>>name; for(a = 0; a!=name.length(); ++a) cout<<int(name[a])<<", "; If I input cat it shows the values 99, 97 116 I want to have the sum of these outputted out instead of those individual values Thanks | |
| yang (33) | |||
| |||
| takumi05 (18) | |
| thanks! I just realised this only works for one word. how would I make so it would show the value of two words eg happy dog thanks | |
| Bazzy (4111) | |
instead of cin>>name use getline(cin,name) so you can get an entire line or get two strings and sum them together | |
| takumi05 (18) | |||
so like that? | |||
Last edited on | |||
| Bazzy (4111) | |
| yes | |
| takumi05 (18) | |
| i get 0 for the ASCII value output | |
| Bazzy (4111) | |
| What input did you give? If you hit enter before entering anything, you'll get an empty string | |
| takumi05 (18) | |||
I put this in so I could put in an input
| |||
| helios (6064) | |
| You don't need to use ignore() when using std::getline(). | |
| takumi05 (18) | |
| I need to for some reason... that loop there is like just one part. I got other parts to the program. | |
| takumi05 (18) | |||
this is the program Ive written. If I input just 'm' for the gender and then input a name the sum outputs 0 if I input 'male' then it outputs and then I input a name it outputs a value but its not the correct ascii value. | |||
| Bazzy (4111) | |
| That is because of your extra characters. you should move cin.ignore before getline or call cin.sync (before getline) | |
| takumi05 (18) | |
| sorry whats "cin.sync"? I havent learn that yet. | |
| Bazzy (4111) | |
| It removes the characters you didn't read from the stream buffer http://www.cplusplus.com/reference/iostream/istream/sync/ | |
| takumi05 (18) | |
| ahhhh okay thank you | |
This topic is archived - New replies not allowed.
