Hello, I am new to this site, as well as to C++. I was hoping someone could help me out with my program, I was hoping to incorporate an if, else statement which we have yet to go over to see if I can. I have basic experience in java so I know some about if, then's but I was wondering if you wouldn't mind helping me out a little with this program:

Please keep in mind that I am a beginner and in a novice C++ class so I would appreciate a flame free discussion :)

double ovalue;

 cout << "Please enter your value and press <enter>: ";
    
    cin >> ovalue;
    
    if ( ovalue == "-> a letter <-" ) ***
    
    {
    cout << "You have entered an unknown number, please try again."
    }
      
    cout << endl; 
    
    else
    {
    
    cout << "The value you have entered is: " << ovalue << ".";
    
    }

*** First off this is not my exact problem, I changed it up a little just in case this sort of thing is unacceptable. And 2 there is a lot more to this code.

Back on track I was wondering if I was anywhere close to being on track? Do I have to declare an integer value at the top to have it spit the error when someone typed in a letter, or what would I change "-> a letter <-" to, in order to make it spit the error when a letter is typed in? I am kind of weird like this, I don't want you to solve everything for me, I would appreciate some hints at first please. I very much appreciate any/all help people are willing to contribute! And if you site does +rep or thanks, expect plenty from me!

Your basic structure is correct. The problem is Line 13. An else must follow directly after its associated if or the connection will be broken. Line 13 breaks that connection. You will need to move that into the block controlled by your if statement.

Due "Truth in Education" (or similarly named) policies that are typically in place at educational institutions, and forum policies, I'm going to refrain from posting code for now.

I will give you this link though.

commented: Thanks for the help +0

Thank you for the reply, yes that was my mistake I saw that after I made the post and is fixed in my main program now. I was wondering if there was a way to have the if statement look for a letter though, like say a user inputs q for the value; I was wondering what to write into the "if (ovalue=='*') part for it to see the q as a problem and throw up the error message? or even if a user typed in 3453g, is there any way to do that? Thanks in advanced.

Edit: I posted this before I saw your edit, thanks for the link. I understand that you cannot post the code and that is fine. From my past java experience it had something similar to what the link has, and I remember that I didn't learn it for a ways into the year, so I think I will just stick to what is asked. Thanks for all your help though, if anyone else has anymore hints they would like to share though, I would welcome pm's, but I'm just going to say problem solved with this.

The situation you describe with regard to attempting to input a letter/character into a numeric variable is a common one. The issue is known as "stream corruption". There are solutions for this, but I'm afraid they are more involved than simply comparing to a character in an if statement. I fear they are a little beyond your understanding at this point and I don't want to confuse you. If you're feeling confident and absolutely must know, here's a link to a thread about it.

It won't help you in this situation, but to compare to a single character, you put the character literal in 'single quotes':

if (inputVar == 'a')
  doSomething();
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.