HELPPPPPP

Im just a beginner, obviously. Ive decided to try to write my own code and i get some errors, obviously this code isnt correct, can anyone fix it?







#include <iostream>

using namespace std;

int main()
{
int a;
int b;
int sum;


cout << "What would you like to do today?" << endl;
cin >> a;

cout << "So you want to " << sum << endl;

cout << "1 Yes or 2 No?" << endl;
cin >> b;
if(b == 1){

cout << "Oh That sounds awesome!" << endl;
}else{cout << "then goodbye" << endl;


}
return 0;
}



This is what happens when i run it.

http://gyazo.com/872e27d5bc335096c1c7dc3312ca5379

And after i put in an answer, everything else comes up and then closes.


and for those who dont like the plain old text here..

http://gyazo.com/3df35af5617c4294d20014fc8bc9ea6b

there is the code in COLOR. WOAH.
Last edited on
I wanted to help you then I saw "faggot ass nigga" and then no longer wanted to
Last edited on
I changed it because my cousin typed it up there. Im 23 years old, i wouldnt intentionally put that.
First, you do realise that you can use the code tags in the format bar to make the code in 'colour' on the website? Also, please use a sane indentation scheme...

Your problems are multiple. Firstly, you are using 'sum' before you gave it a value, so it should be giving you random junk. Now, you are also trying to store a letter ('d') in an integer, so the failbit for cin is set and it ignores all following attempts at input. Here is your code fixed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main() {
    // please use 'sensical' variable names
    std::string todo;
    int choice;

    std::cout << "What would you like to do today? ";
    std::cin >> todo;

    std::cout << "So you want to " << todo << std::endl;

    std::cout << "Yes [1] or No [2]? "
    std::cin >> choice;
    if (choice == 1) {
        std::cout << "Oh, that sounds awesome!\n";
    } else {
        std::cout << "Then, goodbye.\n";
    }

    return 0;
}
erock 88 1+
So i just copied and pasted your code and got this error.

http://gyazo.com/a0af6148961eef3787a45238ba33a4a3

So i typed it my way and got this error.

http://gyazo.com/d964d0af44943b9105916905a8e82a75

std::cout << "Yes [1] or No [2]? ";
Chriscpp this is what happened when i replaced my line with you line of code.

http://gyazo.com/56465c3386d815d16bba7bc6eae2d3c9
NT3 already mentioned you cannot read letters into an int.
That is all his code. So whatever you are saying he said, is wrong i guess.
You changed his "std::string todo" into "int todo".

An int can store any whole number, positive or negative, but that is all. A string can hold multiple characters. When you try and read in "Nothing" you need to store it into a variable capable of storing a string.

Your code also will currently not work because cin will only look at one character at a time. Reading in the word "Nothing" into some string variable and then printing that variable would result in "N". If you want to read in more than one character at a time you must use getline. Googling the phrase "Cplusplus getline" should lead you to some examples.

Hopefully that helps

EDIT:
may have misrepresented cin. I was thinking of reading in white space separated words into one string. Getline isn't necessary here but it's still useful to know about.
Last edited on
I had a typo in my code - line 14 I was missing the semicolon. In your most recent example, you were copying into an integer rather than a string as in my code. If you aren't satisfied, here is my code compiled: http://coliru.stacked-crooked.com/a/b79b98493d029aa6
So i changed "int todo" to "std::string todo;" and it worked fine...for the most part.

When i entered in life, it did exactly what i programmed it to do.

Here:

http://gyazo.com/c6f971838be2537553f98e25fdad5e28

and here:

http://gyazo.com/808ede26e5b79b79245dc2ed149f40cf

but if i entered more than one word, this happens:

http://gyazo.com/aa801a2dcc8b8325d775514eb19189aa

Everything comes up at once and i cannot input anything else.

Here is the code.

http://gyazo.com/6bc8fc29de032d4431256f64f27c6fa6

As I mentioned in my edit cin will only read in until it hits whitespace. To read an entire line you can use getline.

The rreason everything happens at once is it reads up to the space for the first cin, and then it sees more input left to read in, the second word, and uses that for the second cin. It resulted in an error because it tried to read "life" into an int.
Last edited on
Topic archived. No new replies allowed.