Join 136,180 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,043 people online right now. Registration is fast and FREE... Join Now!
I've got the console program working pretty well, but my major problem is that after you input the data, it will output everything really fast and close the console window before you can even read the data that is output. all help or suggestions are very appreciated. here is my code:
CODE
//IT 210 //Instructor: Vijay Kalburgi //Programmer: Jason Petrie //Program assignment 1 //Calculates total points, average of all program scores, average of all test scores, and the average of all points earned in an IT210 course.
void main() //begining of main { //declarations int firstProgramScore, secondProgramScore, thirdProgramScore, fourthProgramScore, fifthProgramScore; int firstTestScore, secondTestScore, thirdTestScore; int totalPoints, averageTestScore, averageProgramScore, totalPointsAverage; int totalProgramScores, totalTestScores; string firstName, lastName;
//Prompt user to enter student's first and last name and their scores
cout<<"Please enter the student's first name and their last name followed by a space: "<<endl; cin>>firstName>>lastName; cout<<"Please enter the student's first program score: "<<endl; cin>>firstProgramScore; cout<<"Please enter the student's second program score: "<<endl; cin>>secondProgramScore; cout<<"Please enter the student's third program score: "<<endl; cin>>thirdProgramScore; cout<<"Please enter the student's fourth program score: "<<endl; cin>>fourthProgramScore; cout<<"Please enter the student's fifth program score: "<<endl; cin>>fifthProgramScore; cout<<"Please enter the student's first test score: "<<endl; cin>>firstTestScore; cout<<"Please enter the student's second test score: "<<endl; cin>>secondTestScore; cout<<"Please enter the student's third test score: "<<endl; cin>>thirdTestScore; cout<<"Press enter to calculate the student's total points and averages.."; //calculating averages and total points totalProgramScores = firstProgramScore + secondProgramScore + thirdProgramScore + fourthProgramScore + fifthProgramScore; totalTestScores = firstTestScore + secondTestScore + thirdTestScore; totalPoints = totalProgramScores + totalTestScores; averageTestScore = totalTestScores / 3; averageProgramScore = totalProgramScores / 5; totalPointsAverage = totalPoints / 8;
//display calculation results to user
cout<<firstName<<" "<<lastName<<"'s total points for the class are: "<<totalPoints<<endl; cout<<"Their total program score is: "<<totalProgramScores<<endl; cout<<"Their total test score is: "<<totalTestScores<<endl; cout<<"Their average program score is: "<<averageProgramScore<<endl; cout<<"Their average test score is: "<<averageTestScore<<endl; cout<<"Their average of all scores is: "<<totalPointsAverage<<endl; cout<<"press enter to close the window.";
A common solution is to put a getchar(); as the last line before the end your main method. This will pause your program, while it waits for a key to be pressed.
If this is a windows based system that you are writing the application for then you can also use system("PAUSE");.
You do not need the endl at the end of each question. Code is corrected below.
QUOTE(Stiple @ 4 Jul, 2007 - 07:11 PM)
I've got the console program working pretty well, but my major problem is that after you input the data, it will output everything really fast and close the console window before you can even read the data that is output. all help or suggestions are very appreciated. here is my code:
CODE
//IT 210 //Instructor: Vijay Kalburgi //Programmer: Jason Petrie //Program assignment 1 //Calculates total points, average of all program scores, average of all test scores, and the average of all points earned in an IT210 course.
void main() //begining of main { //declarations int firstProgramScore, secondProgramScore, thirdProgramScore, fourthProgramScore, fifthProgramScore; int firstTestScore, secondTestScore, thirdTestScore; int totalPoints, averageTestScore, averageProgramScore, totalPointsAverage; int totalProgramScores, totalTestScores; string firstName, lastName;
//Prompt user to enter student's first and last name and their scores
cout << "Please enter the student's first name and their last name followed by a space: "; cin >> firstName >> lastName;
cout<<"Please enter the student's first program score: "; cin >> firstProgramScore;
cout << "Please enter the student's second program score: "; cin >> secondProgramScore;
cout << "Please enter the student's third program score: "; cin >> thirdProgramScore;
cout << "Please enter the student's fourth program score: "; cin >> fourthProgramScore;
cout << "Please enter the student's fifth program score: "; cin >> fifthProgramScore;
cout<<"Please enter the student's first test score: "; cin>>firstTestScore;
cout<<"Please enter the student's second test score: "; cin >> secondTestScore;
cout << "Please enter the student's third test score: "; cin >> thirdTestScore;
cout << endl << endl << firstName <<" "<< lastName <<"'s total points for the class are: "<< totalPoints << endl; cout<<"Their total program score is: "<<totalProgramScores<<endl; cout<<"Their total test score is: "<<totalTestScores<<endl; cout<<"Their average program score is: "<<averageProgramScore<<endl; cout<<"Their average test score is: "<<averageTestScore<<endl; cout<<"Their average of all scores is: "<<totalPointsAverage<<endl; //end of main }
I had tried the getch() function previously and it didn't fix the problem. it turns out that the problem was that i am using microsoft visual c++ 2005 as my c++ software, and it has some sort of weird bug that unless you run your program from an actual console command, your program that you execute from double-clicking on the executable will run and immediately close. The compiler that ive been using in class is borland which i hadnt had any problems with. there do seem to be other bugs with the microsoft software which doesnt display and of the output formatting such as setw() and setfill(). have any of you had similar problems with the microsoft software? It's making me almost glad that I got the software free with my textbook.
I've got the console program working pretty well, but my major problem is that after you input the data, it will output everything really fast and close the console window before you can even read the data that is output. all help or suggestions are very appreciated. here is my code:
CODE
//IT 210 //Instructor: Vijay Kalburgi //Programmer: Jason Petrie //Program assignment 1 //Calculates total points, average of all program scores, average of all test scores, and the average of all points earned in an IT210 course.
void main() //begining of main { //declarations int firstProgramScore, secondProgramScore, thirdProgramScore, fourthProgramScore, fifthProgramScore; int firstTestScore, secondTestScore, thirdTestScore; int totalPoints, averageTestScore, averageProgramScore, totalPointsAverage; int totalProgramScores, totalTestScores; string firstName, lastName;
//Prompt user to enter student's first and last name and their scores
cout<<"Please enter the student's first name and their last name followed by a space: "<<endl; cin>>firstName>>lastName; cout<<"Please enter the student's first program score: "<<endl; cin>>firstProgramScore; cout<<"Please enter the student's second program score: "<<endl; cin>>secondProgramScore; cout<<"Please enter the student's third program score: "<<endl; cin>>thirdProgramScore; cout<<"Please enter the student's fourth program score: "<<endl; cin>>fourthProgramScore; cout<<"Please enter the student's fifth program score: "<<endl; cin>>fifthProgramScore; cout<<"Please enter the student's first test score: "<<endl; cin>>firstTestScore; cout<<"Please enter the student's second test score: "<<endl; cin>>secondTestScore; cout<<"Please enter the student's third test score: "<<endl; cin>>thirdTestScore; cout<<"Press enter to calculate the student's total points and averages.."; //calculating averages and total points totalProgramScores = firstProgramScore + secondProgramScore + thirdProgramScore + fourthProgramScore + fifthProgramScore; totalTestScores = firstTestScore + secondTestScore + thirdTestScore; totalPoints = totalProgramScores + totalTestScores; averageTestScore = totalTestScores / 3; averageProgramScore = totalProgramScores / 5; totalPointsAverage = totalPoints / 8;
//display calculation results to user
cout<<firstName<<" "<<lastName<<"'s total points for the class are: "<<totalPoints<<endl; cout<<"Their total program score is: "<<totalProgramScores<<endl; cout<<"Their total test score is: "<<totalTestScores<<endl; cout<<"Their average program score is: "<<averageProgramScore<<endl; cout<<"Their average test score is: "<<averageTestScore<<endl; cout<<"Their average of all scores is: "<<totalPointsAverage<<endl; cout<<"press enter to close the window.";
//end of main
Hey Jason a simply suggestion try this. At the end of your program enter this
CODE
system("pause");
This will stop the system automatically closing the window until you press a key.