0
// ConsoleApplication2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iomanip> #include <iostream> #include <string> using namespace std; int main() { int n; // sportsmans number int kartai=0; // kartai - times int t=0; // how many points sportsman gets int tMin=0, tMax=0; // smallest and biggest points cout << "Parasykite kiek isviso sportininku dalyvavo" << endl; //Inputing how many sportsman played cin >> n; // while (kartai < n) { cout << "irasykite kiek tasku sportininkai gavo"; // inputing how many points sportsmans got cin >> t; if (kartai == 0) { t = tMin; t = tMax; } else { if (t > tMax) tMax = t; else if (t < tMin) tMin = t; } kartai++; } cout << "didziausias skaicius buvo" << tMax << endl; // biggest score cout << "maziausias skaicius buvo" << tMin << endl; // smallest score } 

So after putting all how many points sportsmens gots in while loop, program quits never putting maximum and minimum points, nor does show any cout after while

5
  • 3
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: How to debug small programs Commented Mar 21, 2017 at 18:23
  • 1
    If you're running this in Visual Studio, the console window closes very quickly, before you get a chance to see it. A popular way of keeping it open is to read some input at the end of main. Commented Mar 21, 2017 at 18:24
  • 1
    You should look more closely at your assignments – a couple of them are in the wrong direction. Commented Mar 21, 2017 at 18:25
  • Oh god, it was so embarassing of me. The reason program closed quickly was that I started debugging, not start without debugging. Either way I have to find why smallest score is wrong. Thanks everyone for trying to help me. Commented Mar 21, 2017 at 18:40
  • 1
    stackoverflow.com/questions/24776262/pause-console-in-c-program Commented Mar 21, 2017 at 19:11

3 Answers 3

3

Your program quits after the loop simply because it is done (well, except for the two cout statements, but they are over in milliseconds).

If you run the program from the commandline you should see the output and get your prompt back when the program terminates. If you run the program from the GUI it may open and close a terminal window with the output so fast that you simply do not see it.

Sign up to request clarification or add additional context in comments.

Comments

0

At the end of your main you could pause the console:

cout << "didziausias skaicius buvo" << tMax << endl; // biggest score cout << "maziausias skaicius buvo" << tMin << endl; // smallest score // Call a pause to prevent main from exiting. system("pause"); 

main is your final function before exiting the application. The output is there, it's just that you don't have time to see it. This is one way to prevent your application from exiting so that you have time to read the output in the console.

2 Comments

I would just like to comment, using system("pause") might be fine for a homework assignment or learning project as this question is probably doing - but in the real world this should try to be avoided. Alternatives could be to use getchar() or cin. However none of these will address the fact that the app could terminate early if an error is thrown - and then it won't be left open. A clever way I saw posted on a forum around this is to create an instance of a class at the start of main whose destructor uses any of the afformentioned techniques.
Yes, that's a fair statement. I too made the assumption that this was a learning project without thinking further. I'll have to consider the other options in the future.
0

See my changes below. As far as I can see, you only need to include iostream. On the if(kartai == 0) branch it seems as if you mixed up the assignments. To prevent the the window to close add cin.ignore(); and cin.get();


#include <iostream> using namespace std; int main() { int n; // sportsmans number int kartai = 0; // kartai - times int t = 0; // how many points sportsman gets int tMin = 0, tMax = 0; // smallest and biggest points cout << "Parasykite kiek isviso sportininku dalyvavo: "; //Inputing how many sportsman played cin >> n; while (kartai < n) { cout << "\nirasykite kiek tasku sportininkai gavo: "; // inputing how many points sportsmans got cin >> t; if (kartai == 0) { tMin = t; tMax = t; } else { if (t > tMax) tMax = t; else if (t < tMin) tMin = t; } kartai++; } cout << "didziausias skaicius buvo: " << tMax << endl; // biggest score cout << "maziausias skaicius buvo: " << tMin << endl; // smallest score cin.ignore(); cin.get(); return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.