Hi. I'm trying to make a simple question program, my first question doesn't work (if you enter the wrong answer/an answer that isn't an integer, you get stuck in an infinite loop).
However, my second question works fine even though the code is quite similar.
Sorry if I'm being a bit vague, here is the code so you can try it for yourself.
Any help is appreciated. :(

Also any suggestions on how I could improve my code? I am new. ^^

#include <iostream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

using namespace std;

int main()
{
  int integervalue;
  string chtest;
  char X;

/*First Question*/

do
{

  cout<<"Please type a number then press Enter: \n\n\n";
  cin>> integervalue;
  if(integervalue>-9999999999999)
  {
  cout<<"\n\n\nYay!\n\n\n";
  }
  else
  {
  cout<<"\n\n\nTry again.\n\n\n";
  }
  } while (integervalue == X);
  cin.get();
  cout<<"Press Enter to go to the next question.";
  cin.get();

/*Clear the CMD*/

system("cls");

/*Second Question*/

do
{

  cout<<"Please type the word 'Cplusplus' then press Enter: \n\n\n";
  cin>> chtest;
  if(chtest=="Cplusplus")
  {
  cout<<"\n\n\nYay!\n\n\n";
  }
  else
  {
  cout<<"\n\n\nTry again.\n\n\n";
  }
  } while (chtest != "Cplusplus");
  cout<<"Press Enter to exit.";

}

You are comparing your integer to X. What is X? In this program it is not set as anything.

Well I assumed X (being a 'char') is any letter.
Ok, so the solution to this would be...

} while (isalpha(integervalue));

Right?

Am I making any sense? :P

Well I assumed X (being a 'char') is any letter.

No, it's any value 0 to 255. But which one? If you don't set it, you have no idea.

Ok, so the solution to this would be...

} while (isalpha(integervalue));

Right?

Am I making any sense? :P

That looks like it would work. Did it?


Also: if(integervalue>-9999999999999) What?!?!?!!

Here is what I got from http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/...
I think that you should just input to the int and then check if the failbit was set (you can do this using the fail() function).

Yeah I have no idea how to use that :(
Could you try putting it in my code so I can learn from it please?

@WaltP Well yes it worked, but it still doesn't solve my loop problem.
That insane integer value was just used to specify any number, the best way I could think of. hehe..

while (!f.fail())
{
    cin>>integervalue;
}

Well, why did you set x as a char? And I dont think line 18 and 42 are supposed to be there.

Well, why did you set x as a char? And I dont think line 18 and 42 are supposed to be there.

I already explained above why I did that.
And lines 18 and 42 are supposed to be there for my do-while loop, though I think I've done it wrong because if you get the first question wrong the loop doesn't stop.

@Labdabeta
That gave me 7 errors. ^^

Oh ok. I just usually use for(;;) for an infinite loop

Sorry but that smily icon is supposed to be for(;; )

Well, I just used the first thing I learned, which just happened to be do-while.
Would 'for' work in this situation?

Also examples are needed if possible :P
I am a visual learner

A For loop would be a bad idea here, I think do while is probably good for the situation.

I'd like to help, but I don't fully understand what you want to do with question 1. What exactly is this supposed to do?

Ok, what question 1 is, is it asks the user to enter an integer, any number.
If they enter a number and press enter, you go to question 2, as you can see if you run the program.

But if you don't enter an integer, you should loop back to the beginning of the question.

That's all I'm trying to do. :)

Well, you have to use the break function to exit the loop.

Wait, I don't think it's a loop, if you look at the attachment, it is just the second question showing over and over very quickly. Not the first question looping.
I don't know...

Sorry to be such an idiot.

The only requirement to break out of the loop is the condition in the while has to be false.

I don't really get why it's working the way it does.

One solution could be to read the input as a string, and to check if each char in the string is an int. That sound good?

Yeah. I'm not really familiar with the true and false functions.

So I did it like this (I think it is how you described I should do it, correct me if I'm wrong) and I got the error: no matching function for call to 'isalpha(std::string&)'|

do
{

  cout<<"Please type a number then press Enter: \n\n\n";
  cin>> integervalue;
  if(isdigit(integervalue))
  {
  cout<<"\n\n\nYay!\n\n\n";
  }
  else
  {
  cout<<"\n\n\nTry again.\n\n\n";
  }
  } while (isalpha(integervalue));
  cin.get();
  cout<<"Press Enter to go to the next question.";
  cin.get();

Ok, when you do cin>>integervalue; It automatically stores your input into an integer. So if you input 'g', it will give you some value stored as an int, iirc.

if you change X to a string, and do cin>>X, then use integervalue=atoi(X); It will save it as an integer. I'm trying to think of the simplest way to check to make sure every character is a digit though.

What happened when you tried

1.
while (!f.fail())
2.
{
3.
cin>>integervalue;
4.
}

For question 1?

error: cannot convert 'std::string' to 'const char*' for argument '1' to 'int atoi(const char*)'|

string X;
  int integervalue;
  string chtest;
............
out<<"Please type a number then press Enter: \n\n\n";
  cin>> integervalue=atoi(X);
  if(isdigit(integervalue))

Like that? Sorry if I don't understand fully, I started learning C++ today and it took me hours to write this program.

Also for the 'fail' I got this error:
error: 'f' was not declared in this scope|

You need to add a #include <string> up at the top of the program

You need to add a #include <string> up at the top of the program

Yeah that's already there.

I'm thinking if you use atoi(X.c_str()) it will work, I'm trying to reinstall a compiler to check.

Ok thanks for your help, I couldn't get atoi that to work, hopefully you can.
I've done the best I can with my basic understanding of C++.
I am definitely repping you up, if you get it to work or not for your efforts.

Ok, Mike, I hope you are still reading. This works except when you put 0 in for the int, because of how atoi works.

#include <iostream>
      #include <string>
      #include <windows.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <ctype.h>
       
      using namespace std;
       
      int main()
      {
      int integervalue;
      string chtest, X;
       
      /*First Question*/
       
      do
      {
      cout<<"Please type a number then press Enter: \n\n\n";
      cin>> X;
      integervalue=atoi(X.c_str());
      if(integervalue!=0){
          cout<<"\n\n\nYay!\n\n\n";
      }
      else{
           cout<<"\n\n\nTry again.\n\n\n";
          }
      } while (integervalue == 0);

      cout<<"Press Enter to go to the next question.";


      /*Clear the CMD*/

      system("cls");

      /*Second Question*/

      do
      {
      cout<<"Please type the word 'Cplusplus' then press Enter: \n\n\n";
      cin>> chtest;
      if(chtest=="Cplusplus")
      {
      cout<<"\n\n\nYay!\n\n\n";
      }
      else
      {
      cout<<"\n\n\nTry again.\n\n\n";
      }
      } while (chtest != "Cplusplus");
      cout<<"Press Enter to exit.";
      }

Thank you so much. :)
You're a genius.

Using cls to clear teminal is consired bad, antisocial style as you are destroying output from any previously run command. That is also only working in Windows so you make your program unportable without reason.

Hmm ok but that isn't really 'constructive' criticism is it if you're not telling me any better option...

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.