I'm trying to come up with a small block of code that simply takes a character value that is input by the user and then use an "if" statement to determine what is excecuted depending on what is entered.

I just started C++ this week and have no prior experience so please be gentle! :)

This is wrong, but something to the effect of this;

int input; 

  cout << "Enter t or f: ";
  cin >> input;

    if(input = t) {
        cout << "True";
    if(input = f) {
        cout << "False";
// if anything other than t or f is entered, displays error message and loops back for a correct response.

Recommended Answers

All 10 Replies

Posting Questions Lesson number 1:
"This is wrong" doesn't cut it. What is wrong? Always provide details. Only you know what happened, so you need to tell us
a) what happened
b) what should have happened.
Remember, we cannot see your screen. You need to post all information that is needed to figure out the problem.

In this case t and f are variables. You want characters 't' and 'f'

//Provides ability to write to the dos console (cin/cout)
#include<iostream>

using namespace std;

int main()
{
     int input = 0; 

     cout << "Enter t or f: ";
     cin >> input;

     do{

          if(input == 't') 
          {
               cout << "True";
          }
          else if(input == 'f') 
          {
               cout << "False";
          }
          else
          {
               cout << "Incorrect entry.  Try again. \n";
          }
     
     }while(input != 't' && input != 'f');

     return 0;
}
commented: You need to STOP posting code for people. Their job is to write the code. You job is to stop trying to impress everyone with your coding prowess. -2

"This is wrong" doesn't cut it. What is wrong? Always provide details.]

I don't have any code because I don't know how to write it correctly. Obviously that code will not work as is but i posted that, meant as a pseudo code for something I am trying to do, thats what I meant by

but something to the effect of this;

Clinton Portis, I get a uninitialized local variable t and f error when I try that. I tried defining t and f as both characters and integers, but got the same error both times.

So I guess the problem now is that if I wanted to write the code so that it would read "1" for true and "2" for false, I could, but I need it to see "t" as true and "f" for false.

Look up the difference between int and char
Look up how an if statement is designed
Look up how to specify a character value vs integer value vs string value

I've been back through my book 5 or 6 times now, I'm just not seeing it!

This is for a larger project and I'm stuck on this specific part of it , all i'm looking for is an example of some kind to change a user input of a "t" to an integer in the program such as a "1" If I can SEE it in use i will understand it 100% more than I do now.

I've been back through my book 5 or 6 times now, I'm just not seeing it!

This is for a larger project and I'm stuck on this specific part of it , all i'm looking for is an example of some kind to change a user input of a "t" to an integer in the program such as a "1" If I can SEE it in use i will understand it 100% more than I do now.

You can always try using strings like this:

string input;
int main(){

cin >> input;
if (input == "t"){
...
}
else if (input == "f"){
...
}

return 0;
}

or however you want to use it.

look at my previous post. the problem is in line #8. make the correction and let us know what you did to fix the problem.

look at my previous post. the problem is in line #8. make the correction and let us know what you did to fix the problem.

I assume you did that on purpose now?

wow.... I feel extremely stupid now.:sad:

yeah, just changed the integer to character and that fixed it.... thanks for putting up with me I suppose :)

Clinton
line #8 - string input; not int input;
and move line #13 do{ to line #9

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.