My command prompt will not stay open

Pages: 12
Jan 17, 2011 at 7:41pm
Here is the code:

#include <iostream>

using namespace std;

int main()

{
int iPlayers;

int iHours;

int iPoints;

int iGames;

int iaverage_player_hrs

cout << "Enter in The Total Number of Players: " << endl;

cin >> iPlayers;

cout << "Enter in The Total Number of Hours Played: " << endl;

cin >> iHours;

cout << "Enter in The Total Number of Points Scored: " << endl;

cin >> iPoints;

cout << "Enter in The Total Number of Games Played: " << endl;

cin >> iGames;

int iaverage_player_hrs = iHours/iPlayers;

cout <<" The average player Hours are: " << iavarage_player_hrs << endl << endl;

int iaverage_player_points = iPoints/iPlayers;

cout <<" The average player points are: "<< iaverage_player_points<<endl<<endl;

int iaverage_points_game = iPoints/iGames;

cout <<" The average game points are: "<< iaverage_points_game <<endl<<endl;


int iaverage_points_per_hour_per_player = iaverage_player_points/iaverage_player_hrs;
cout << " the average points per hour per player are: " << iaverage_points_per_hour_per_player << endl;

system("PAUSE");

return 0;

}


The command prompt does not stay open so I can enter in information.

Any suggestions?
Jan 17, 2011 at 8:33pm
Does it compile? I see an error but I'm not sure if it's just a typo. Assuming it compiles, it ought to allow you to input with no problems.
Jan 17, 2011 at 8:40pm
Whats the typo?

And it won't stay open.
Jan 17, 2011 at 8:42pm
Does it compile? If the code above is exactly what you're trying to build, then it's broken and it won't compile because you're missing a semi-colon on the end of int iaverage_player_hrs, which I suspect it the typo spotted by eagle-eyed Zhuge.

You've also used system without including the appropriate header.
Last edited on Jan 17, 2011 at 8:45pm
Jan 17, 2011 at 8:43pm
You are missing a semicolon on one of your first lines. If it won't stay open and there are no errors, then it could be you have a problem not relating to the code. Possibly it could be your IDE or something, but it works fine for me.
Jan 17, 2011 at 8:49pm
Alright I have put the ; at the int iaverage_player_hrs.

But it still does not let me input data into the command prompt.

if there is something wrong with my IDE, what is it?

You've also used system without including the appropriate header. (How do I do this?)
Last edited on Jan 17, 2011 at 8:51pm
Jan 17, 2011 at 8:51pm
try CRTL+F5 (if using Visual Studio)
Jan 17, 2011 at 9:15pm
You've also used system without including the appropriate header. (How do I do this?)


#include <cstdlib>
Jan 17, 2011 at 9:37pm
are you compiling this under windows? if not system commands are not portable. you could try adding to the end to try and make it stay open. i dont think its your ide i think its your use of system.

1
2
std::cin.sync();
std::getchar();

Last edited on Jan 17, 2011 at 9:38pm
Jan 17, 2011 at 9:44pm
Nope I did what Moschops and acorn said it still does not stay open:

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{
int iPlayers;

int iHours;

int iPoints;

int iGames;

int iaverage_player_hrs;

cout << "Enter in The Total Number of Players: " << endl;

cin >> iPlayers;

cout << "Enter in The Total Number of Hours Played: " << endl;

cin >> iHours;

cout << "Enter in The Total Number of Points Scored: " << endl;

cin >> iPoints;

cout << "Enter in The Total Number of Games Played: " << endl;

cin >> iGames;

int iaverage_player_hrs = iHours/iPlayers;

cout <<" The average player Hours are: " << iavarage_player_hrs << endl << endl;

int iaverage_player_points = iPoints/iPlayers;

cout <<" The average player points are: "<< iaverage_player_points<<endl<<endl;

int iaverage_points_game = iPoints/iGames;

cout <<" The average game points are: "<< iaverage_points_game <<endl<<endl;


int iaverage_points_per_hour_per_player = iaverage_player_points/iaverage_player_hrs;
cout << " the average points per hour per player are: " << iaverage_points_per_hour_per_player << endl;

std::cin.sync();

std::getchar();

system("PAUSE");

return 0;

}


Also I am using Windows Vista, Visual C++ 2008 Express
Last edited on Jan 17, 2011 at 9:45pm
Jan 17, 2011 at 9:45pm
Do you actually get to enter any data at all? When I try to compile you code as in your post before this one, I get the following errors and it does not compile, so I am astounded that it compiles for you.


bad3.cpp:36: error: redeclaration of ‘int iaverage_player_hrs’
bad3.cpp:18: error: ‘int iaverage_player_hrs’ previously declared here
bad3.cpp:38: error: ‘iavarage_player_hrs’ was not declared in this scope
bad3.cpp:54: error: ‘getchar’ is not a member of ‘std’
Last edited on Jan 17, 2011 at 9:46pm
Jan 17, 2011 at 9:48pm
Wow I do not get any errors.

It says this:========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

No the screen barely opens and immediately closes. No chance to enter input
Jan 17, 2011 at 9:50pm
I'm not convinced that you're actually compiling the code you think you're compiling. Have you added the cpp file to the project?
Last edited on Jan 17, 2011 at 9:50pm
Jan 17, 2011 at 9:50pm
well your getting the not is a member of std because the standard namespace was already blown open because of using namespace std; he will have to remove the std:: prefix. i didnt check the other errors
Jan 17, 2011 at 9:52pm
I am compiling the code above I have found this tho:

warning C4101: 'iPlayers' : unreferenced local variable

exe not found or not built by the last incremental link; performing full link

1>Flalich_1 - 0 error(s), 1 warning(s)

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
Jan 17, 2011 at 9:53pm
well your getting the not is a member of std because the standard namespace was already blown open because of using namespace std; he will have to remove the std:: prefix.

It has nothing to do with already declaring using namespace std. getchar actually is not a member of std namespace.
Jan 17, 2011 at 9:59pm
I closed C++ and made a new project and pasted the aforementioned code and now i receive this:

1>------ Build started: Project: ,m, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(1) : error C2143: syntax error : missing ';' before '<'
1>c:\users\documents\visual studio 2008\projects\,m\,m\main.cpp(1) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(20) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(20) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(22) : error C2065: 'cin' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(24) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(24) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(26) : error C2065: 'cin' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(28) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(28) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(30) : error C2065: 'cin' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(32) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(32) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(34) : error C2065: 'cin' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(36) : error C2086: 'int iaverage_player_hrs' : redefinition
1> c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(18) : see declaration of 'iaverage_player_hrs'
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(38) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(38) : error C2065: 'iavarage_player_hrs' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(38) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(38) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(42) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(42) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(42) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(46) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(46) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(46) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(50) : error C2065: 'cout' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(50) : error C2065: 'endl' : undeclared identifier
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(52) : error C2039: 'cin' : is not a member of 'std'
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(52) : error C2065: 'cin' : undeclared identifier
1>c:\users\documents\visual studio 2008\projects\,m\,m\main.cpp(52) : error C2228: left of '.sync' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(54) : error C2039: 'getchar' : is not a member of 'std'
1>c:\users\\documents\visual studio 2008\projects\,m\,m\main.cpp(54) : error C3861: 'getchar': identifier not found
1>Build log was saved at "file://c:\Users\Lalich\Documents\Visual Studio 2008\Projects\,m\,m\Debug\BuildLog.htm"
1>,m - 32 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Jan 17, 2011 at 10:01pm
oh ok. i thought it was. if i read the tooltip it says its not. strangely enough though i dont get an error in vc 2010 express using it that way.
Last edited on Jan 17, 2011 at 10:15pm
Jan 17, 2011 at 10:01pm
Did you miss any of the code when you pasted it? Maybe at the start?
Jan 17, 2011 at 10:12pm
Alright you guys helped alot now it actually works.

Now it sadly has a new problem:

Run-Time Check Failure #3 - The variable 'iavarage_player_hrs' is being used without being initialized.

What does this mean?
Pages: 12