7 Replies - 4596 Views - Last Post: 05 July 2007 - 07:09 AM Rate Topic: -----

#1 Stiple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-July 07

Output doesn't stay on screen?!

Post icon  Posted 04 July 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:
//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.

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

	
	

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
}


Is This A Good Question/Topic? 0
  • +

Replies To: Output doesn't stay on screen?!

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Output doesn't stay on screen?!

Posted 04 July 2007 - 09:50 PM

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");.
Was This Post Helpful? 0
  • +
  • -

#3 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Output doesn't stay on screen?!

Posted 04 July 2007 - 10:00 PM

You can do something like this
#include <iostream>
#include <limits>
 
int main() {
   
  // Rest of the code	 
  
  //Clean the stream and ask for input
  std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  std::cin.get();
  }

This post has been edited by Xing: 04 July 2007 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 barnwillyb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: Output doesn't stay on screen?!

Posted 04 July 2007 - 10:15 PM

You do not need the endl at the end of each question. Code is corrected below.



View PostStiple, on 4 Jul, 2007 - 07:11 PM, said:

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:
//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.

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

	
	

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;

	//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 << 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
}

Was This Post Helpful? 0
  • +
  • -

#5 Stiple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-July 07

Re: Output doesn't stay on screen?!

Posted 04 July 2007 - 10:48 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Output doesn't stay on screen?!

Posted 05 July 2007 - 02:38 AM

do this: place after evrey cin>>; a cin.ignore(); and place at the end of the code cin.get();
Was This Post Helpful? 0
  • +
  • -

#7 oxygen8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-February 07

Re: Output doesn't stay on screen?!

Posted 05 July 2007 - 06:32 AM

View PostStiple, on 4 Jul, 2007 - 07:11 PM, said:

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:
//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.

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

	
	

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
 
system("pause");


This will stop the system automatically closing the window until you press a key.
Was This Post Helpful? 0
  • +
  • -

#8 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Output doesn't stay on screen?!

Posted 05 July 2007 - 07:09 AM

oxygen8, please follow this thread
http://www.dreaminco...wtopic30088.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1