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
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
Advertisement
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.
I tested this with success, it should work for you:

#include <iostream>

int main(){
std::cin.get();
return 0;
}
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
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;}
*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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
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.
cin.get() //gets the whole line
cin.get(variable)//gets specified variable..

this is what I found
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)
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
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.

This topic is closed to new replies.

Advertisement