• Advertisement
Sign in to follow this  

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

This topic is 4431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The first problem is when your using cin on count you are overwritting the value of count.

define:

char somevar;
cin >> somevar;


instead.

Share this post


Link to post
Share on other sites
try system("PAUSE"); which works in windows though I'm uncertain the system command in other operating systems

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
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?


If i have it set on char then x is the only one that will work and it will just terminate if i use something else(except for space which is just going down a blank line)
if i have it for int it dosent matter what number i use it still goes up a number

Share this post


Link to post
Share on other sites
Quote:
Original post by M2tM
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()){;}


yea i tried, you still need a char... and i have no clue what the other things are, so I don't really want to use them.

Share this post


Link to post
Share on other sites
I tested this with success, it should work for you:

#include <iostream>

int main(){
std::cin.get();
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by M2tM
I tested this with success, it should work for you:

#include <iostream>

int main(){
std::cin.get();
return 0;
}


this is my code and it will not work(I don't have std::cin.get() cause i said using at the top


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
char count;
count = 1;


cout<<"Please press ENTER to begin counting."<<endl;
cin>>count;
while (count <= 25) {
cout<<"PRESS ENTER" <<endl;
cout<< count <<endl;
cin.get(count);
return 0;
count ++;
}
system("PAUSE");
return EXIT_SUCCESS;
}




__________

Edit:

I may see my problem

_____
reedit:

was wrong... heres new code again..

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
char count;
count = 1;


cout<<"Please press ENTER to begin counting."<<endl;
cin.get(count);
while (count <= 25) {
cout<<"PRESS ENTER" <<endl;
cout<< count <<endl;
cin.get(count);
return 0;
count ++;
}
system("PAUSE");
return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites
*smacks his forehead*

Dude.


#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.get();
while (count <= 25) {
cout<<"PRESS ENTER" <<endl;
cout<< count <<endl;
cin.get();
//return 0;
count ++;
}
system("PAUSE");
return 0;
}






It's cool that you're new, but learn to learn. This is a question good ol' google could answer easily.

I should also mention that cin.get() is different from cin.get(someChar) which expects a character to be stored... you don't need to store the character, you just want to get an enter keypress. Also, you should keep the count as an int, I don't know why you blindly changed it when misinterpreting advice.

You need to read a beginners book. There are several good ones, just look around your local bookstore.

Share this post


Link to post
Share on other sites
Please try to understand snippets of code you are given rather than just tossing them in.

cin.get() says "read one character from the input". If there aren't any characters available, that will wait until something is typed in. Because the standard input is line-buffered, it will specifically wait until there is a return keypress, but then will only read one character (so if you type foo and then press return, then it will wait until the return, then read 'f', then on the next loop 'o' is available so it will go right on ahead; then on the next loop the second 'o' is available so it will go right on ahead; then on the next loop the newline is available; and then finally it will wait on the loop after that).

return 0 says "I am done with the calculations of this function; the result is 0". In main(), that exits the program, because there is nowhere to return to. That's clearly not what you want.

Share this post


Link to post
Share on other sites
Quote:
Original post by M2tM
*smacks his forehead*

Dude.

*** Source Snippet Removed ***

It's cool that you're new, but learn to learn. This is a question good ol' google could answer easily. cin.get() is different from cin.get(someChar) which expects a character.

You need to read a beginners book. There are several good ones, just look around your local bookstore.


First of all didn't see the code their at all b4.
Secondly I appreciate the help you guys are giving me, even though you may think I don't.
Thirdly I got a book right here in my lap...(C++ Without Fear by Brian Overland) and I have read into the cin sections and nowhere does it talk about cin.get() in any forum(yes i checked the index aswell)

Share this post


Link to post
Share on other sites
yes, cin.get() is better in this case because you are pressing enter, not entering individual values. I edited my original post because I bungled an explanation. This way if a user types

fooooo[enter]

it should just go once as opposed to 6 times.

And you really don't need to store the value in this case, but if you wanted to you could. I apologize for being vague.



*edit: and that's good that you have a book, just keep working on the exercises. Suppliment that with some google searches like you just did for cin.get and you'll do well.

Share this post


Link to post
Share on other sites
C++ reference: istream. Since cin is part of the istream, you can use its functions. look at the overloaded functions for get() at that page.

Quote:

cin.get() //gets the whole line

cin.getline() is what you were thinking of.

Share this post


Link to post
Share on other sites
ditto on M2tM's code....You want to ignore user input, you don't want to wait for it, so dont pass anything to cin.get() so that the user just has to hit <enter>

Share this post


Link to post
Share on other sites



Im sorry I had to ask.... but why am i getting this now?(im still trying to understand what you said so i messed around with it and got

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
char count;
count = 1;


cout<<"Please press ENTER to begin counting."<<endl;
cin.get(count);
while (count <= 25) {
cout<<"PRESS ENTER" <<endl;
cout<< count <<endl;
cin.get(count);
count ++;
}
system("PAUSE");
return EXIT_SUCCESS;
}


Share this post


Link to post
Share on other sites
Change
char count = 1;
to
int count = 1;

and
cin.get(count);
to
cin.get();

Explanation:
Since you were outputting count and it was a char it was trying to output the value of char(1), char(2), char(3) which aren't numbers but characters. Changing count to an int will allow cout to output count as a number.

You need to change cin.get(count) to just cin.get() because you don't want to read in the input and store it in count. You only want to read in the input and throw it away.

Share this post


Link to post
Share on other sites
you can't see that very well...

Im just going to give up on this for the time being... its obviously something I have yet to learn and I will get in the future.

Thanks for everyones help really, though you think you have done nothing you have really helped.

Share this post


Link to post
Share on other sites
If you copy/paste my code it will work exactly as you wanted. Couple that with people's explanations and you can figure it out. Best would be to leave forums alone and just try to work through examples in your book though. Try searching with google for things you can't get to work. You're at such a beginning stage that asking for help is silly because people already have tutorials written specifically about the stuff you are asking for, so it's not like the information isn't out there to find.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement