Why isn't this working the way I wan't it to?(C++)

Started by
29 comments, last by DeathsBargin 18 years, 3 months ago

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int count;
    count = 1;
    
    
    cout<<"Please press ENTER to begin counting."<<endl;
    cin>>count;
    while (count <= 25) {
          cout<<"PRESS ENTER" <<endl;
          cout<< count <<endl;
          cin>>count;
          count ++;
          }
    system("PAUSE");
    return EXIT_SUCCESS;
}

I want it to show you count and then ask you to press enter, after you press enter count should go up by one, display this, and then display, repeat the process till we get to 25. But it won't display anything...
Advertisement
The line "cin >> count;" means input an integer into count. So it will say "Please press ENTER to begin counting." then wait for you to enter an integer(not just an enter) before continuing.
The first problem is when your using cin on count you are overwritting the value of count.

define:

char somevar;
cin >> somevar;


instead.
<a href="http://ruggles.hopto.org>My Personal Website
try system("PAUSE"); which works in windows though I'm uncertain the system command in other operating systems
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
ah, i just tested that your right....*Smacks self in face..* int = integer duh!

So what would I store it in if I wanted it to do a space?
a char...
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Quote:Original post by ChaosX2
The first problem is when your using cin on count you are overwritting the value of count.

define:

char somevar;
cin >> somevar;


instead.


I didn't find any problems other than i need to press and int instead of space..(And obviously thats not what I want)
Quote:Original post by DeathsBargin
I want it to show you count and then ask you to press enter, after you press enter count should go up by one, display this, and then display, repeat the process till we get to 25.


(a) what happens when you type "24[return]" at the prompt?

(b) what happend when you type "0[return]" at the prompt?

(c) what happens when you type "x[return]" at the prompt?

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by M2tM
a char...


space dosen't count as a char
std::cin.get();

may work, though I think you need to actually enter a character there as well... if you have access to conio.h with your compiler you can use while(!kbhit()){;}

__
edit:
a space is most DEFINITLY a char. It may not store it when expecting input, but you can certainly do this to store a char:

char ch = ' ';

my answer was cheeky but correct to your original question.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

This topic is closed to new replies.

Advertisement