Skip to main content
gamedev.net
🔒 Locked

INPUT/OUTPUT Problem

Started by sealevelgames 6 replies 700+ views
Original Post
sealevelgames
sealevelgames
Heres my source code: #include <iostream> #include <string> using namespace std; int main() { string name; int ID; int age; int favgame; cout << "Enter your name "; cin >> name; cout << "Enter your ID number "; cin >> ID; cout << "Enter your age "; cin >> age; cout << "Enter your favorite game "; cin >> favgame; cout << "Hello " << name << " or should I say " << ID << endl; cout << "I like the age of " << age << " and I like the game " << favgame << endl; return 0; } problem is, when I run it, all of the input works fine, but after I press enter after the favorite game line, it closes itself out, and wont output...
ms291052
ms291052
Try
#include <iostream>#include <string>#include <conio.h>using namespace std;int main(){string name;int ID;int age;int favgame;cout << "Enter your name ";cin >> name;cout << "Enter your ID number ";cin >> ID;cout << "Enter your age ";cin >> age;cout << "Enter your favorite game ";cin >> favgame;cout << "Hello " << name << " or should I say " << ID << endl;cout << "I like the age of " << age << " and I like the game " << favgame << endl;while(!kbhit()) {}return 0;}


The problem is the console window closes immediately after the program terminates. Above I added the function kbhit() which checks for a key board key to be hit. The loop above just idly sits there as long as no key has been pressed at the end of the program. Try that. (also note the #include <conio.h>)
sealevelgames
sealevelgames
ok, but now it wont ask me for my favorite game or my age... any help? it skips straight to the answer
sealevelgames
sealevelgames
fixed it! i took the {} quotes, and moved them right next to the (!kbhit()), so it looks like this: (!kbhit()){}

thanks, im upping your rating
sealevelgames
sealevelgames
another problem, it's doing it again, and i didnt change anything:

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
string name;

int ID;
int age;
int favgame;

cout << "Enter your name ";
cin >> name;

cout << "Enter your ID number ";
cin >> ID;

cout << "Enter your age ";
cin >> age;

cout << "Enter your favorite game ";
cin >> favgame;

cout << "Hello " << name << " or should I say " << ID << "I like the age of " << age << " and I like the game " << favgame << endl;

while(!kbhit()){}

return 0;
}

mdeforge
mdeforge
Lol, easy fix you guys. Try putting in system("PAUSE"); before return 0. It pauses the program till the user presses a key. A program ends when it reaches return 0 or the last }, so in order to stop that from happening when the user isn't needed anymore (like inputing) you could use the system("PAUSE"); command.
Doc
Doc
This is a common problem - it comes up every month or so. The easy solution is what Stradigos said, stick system("PAUSE"); in there just before return 0;

Of course, the reality of the situation is not so simple. What system("PAUSE"); does is call the program called PAUSE.EXE which (taking an educated guess) is found in C:\windows\command\. The system() function is standard, but PAUSE.EXE is not. So you end up breaking portability. At this stage you probably don't care about that, though.

What I would like to see is these IDEs fixed. Try looking for an option in your IDE for closing the window when the program terminates and turn it off. This really should be the default behaviour.
Doc
Doc
Quote:
Original post by sealevelgames
int ID;
int age;
int favgame;


Why is favgame an int? Shouldn't it be a string?

If it should be a string this is probably the reason the problem persists. cin tries to read an int but it gets a string, cin dies silently, and keystrokes are left in the buffer which get picked up by kbhit() straight away.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.