I know this piece of code won't work but can you help me make it work?

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

using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    cout << "Do you wish to proceed?(Y/N)" <<endl;
    getchar();

    if (getchar() == 'Y'){
        cout << "Ok good luck! :D" << endl;
    }
    else (getchar() == 'N'){
        cout << "Well bye then:P" << endl;
    }



    return 0;
}



I know this piece of code won't work but can you guys help me to get it working? Basically, I just want the program to do this, whenever it waits for an output, if y is inserted then it prints 'blah', if n is inserted then it prints 'blahblah', (Sorry for this bad explanation but it's 12.44 AM and i'm pretty tired to correct my spelling and poor grammar, I just want to learn as much C++ as possible. :D)
Create a variable and store the info there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdio.h>

using namespace std;
int main()
{
    cout << "Hello world!" << endl;

    char answer;
    cout << "Do you wish to proceed?(Y/N)" <<endl;
    cin >> answer;

    if (answer == 'Y'){
        cout << "Ok good luck! :D" << endl;
    }
    else if (answer == 'N'){ // else if not else
        cout << "Well bye then:P" << endl;
    }

    return 0;
}
Last edited on
Thank you so much, i've been waiting about a half an hour for an answer and you solved my mystery and explained it, thanks! Is there a way for the program to ignore upper-case/lower-case letters. I can't really explain this well but I can show you an example, if I enter the character "n", nothing will pop up because it's lower-case and our program only identifies upper-case "N". Is there a way to ignore case-sensitivity. I know there is a way to do it in Python but I have unfortunately forgotten it.

EDIT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdio.h>

using namespace std;
int main()
{
    cout << "Hello world!" << endl;

    char answer;
    cout << "Do you wish to proceed?(Y/N)" <<endl;
    cin >> answer;

    if (answer == 'Y' || 'y'){
        cout << "Ok good luck! :D" << endl;
    }
    else if (answer == 'N' || 'n'){ // else if not else
        cout << "Well bye then:P" << endl;
    }

    return 0;
}


This obviously work but is there any other way apart from this?
Last edited on
This is how programming works. This is the absolutely best way.

There are lots of tutorials online, and on youtube. Take advantage!

https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83
Last edited on
@Ned757

You could use the toupper(); command
1
2
cin >> answer;
answer=toupper(answer);

Now if the user typed a lowercase letter, it is automatically read as uppercase. And if it was entered in uppercase already, it stays that way.

Also, this if (answer == 'Y' || 'y') is wrong coding. If you are trying to check both upper and lower, it should be if (answer == 'Y' || answer == 'y'). Same for the else if part.
Calling the getchar() function has a side effect, so it would be better to store the result of the first getchar() in a variable and test against the variable;
Topic archived. No new replies allowed.