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.

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..

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:
#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.

So i typed it my way and got this error.

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

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:
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.


and here:

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

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

Here is the code.

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.