Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Output doesn't stay on screen?!

 
Reply to this topicStart new topic

Output doesn't stay on screen?!

Stiple
4 Jul, 2007 - 06:11 PM
Post #1

New D.I.C Head
*

Joined: 4 Jul, 2007
Posts: 6


My Contributions
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.

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

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Output Doesn't Stay On Screen?!
4 Jul, 2007 - 08:50 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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");.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Output Doesn't Stay On Screen?!
4 Jul, 2007 - 09:00 PM
Post #3

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
You can do something like this
CODE
#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: 4 Jul, 2007 - 09:01 PM
User is offlineProfile CardPM
+Quote Post

barnwillyb
RE: Output Doesn't Stay On Screen?!
4 Jul, 2007 - 09:15 PM
Post #4

D.I.C Head
**

Joined: 22 May, 2007
Posts: 55


My Contributions
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.

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



User is online!Profile CardPM
+Quote Post

Stiple
RE: Output Doesn't Stay On Screen?!
4 Jul, 2007 - 09:48 PM
Post #5

New D.I.C Head
*

Joined: 4 Jul, 2007
Posts: 6


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Output Doesn't Stay On Screen?!
5 Jul, 2007 - 01:38 AM
Post #6

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
do this: place after evrey cin>>; a cin.ignore(); and place at the end of the code cin.get();
User is offlineProfile CardPM
+Quote Post

oxygen8
RE: Output Doesn't Stay On Screen?!
5 Jul, 2007 - 05:32 AM
Post #7

New D.I.C Head
*

Joined: 25 Feb, 2007
Posts: 6


My Contributions
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.

#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
CODE

system("pause");

This will stop the system automatically closing the window until you press a key.


User is offlineProfile CardPM
+Quote Post

Xing
RE: Output Doesn't Stay On Screen?!
5 Jul, 2007 - 06:09 AM
Post #8

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
oxygen8, please follow this thread
http://www.dreamincode.net/forums/showtopic30088.htm
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:11AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month