-
run time error?
Hi,
I wrote the following program which can be compiled and built in Visual C++ compiler. However, it returned an error when I run the exe file.
Anyone could help?
Thanks
gogo
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
const int MAX = 10;
string name[MAX] = {0};
int mark[MAX] = {0};
int count = 0;
int sum = 0;
float average = 0;
char finish = 'n';
do
{
cout << "Enter your name : ";
cin >> name[count++];
cout << endl;
cout << "Enter your mark : ";
cin >> mark[count++];
sum += mark[count++];
cout << endl;
cout << "Do you want to add more ? ";
cin >> finish;
} while (tolower(finish)=='y' && (count < MAX));
average = sum / count;
cout << endl;
cout << "The average mark is : " << average << ".\n";
cout << endl;
cout << "Student Name" << setw(5) << "Marks";
cout << "____________" << setw(5) << "_____";
cout << endl;
cout << endl;
for (int i=0; i< count; i++)
cout << name[i] << setw(8) << mark[i] << endl;
return 0;
}
-
In the do-loop you increment count 3 times at once, the name and mark won't fit together anymore and you will overrun the boundaries of your arrays.
Instead of three times count++ only have it once and use count without any plusses afterwards in the one loop.
-
STL string won't take a '0' in a contructor. You don't have to initialise it just do -
string name[MAX];
Also, you only want to increment count once per loop, on the last time it's used.
-
Thanks for both of your help and there is no error during run time now.
I want to add a validation of mark in-between 0-100. I added the if...else condition, but it cannot validate the mark.
What's wrong with my code?
Thanks a lot.
gogo
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
const int MAX = 10;
string name[MAX];
int mark[MAX] = {0};
int count = 0;
int sum = 0;
float average = 0;
char finish = 'n';
do
{
cout << "Enter your name : ";
cin >> name[count];
cout << endl;
cout << "Enter your mark : ";
if ((mark[count] < 0) || (mark[count] > 100)) // don't know how to fix it?
{
cout << "PLs re-enter : " << endl;
cin >> mark[count];
}
else
{
cin >> mark[count];
sum += mark[count++];
}
cout << endl;
cout << "Do you want to add more ? " << endl;
cin >> finish;
} while (tolower(finish)=='y' && (count < MAX));
average = sum / count;
cout << endl;
cout << "The average mark is : " << average << ".\n";
cout << endl;
cout << "Student Name" << setw(20) << "Marks" << endl;
cout << "____________" << setw(20) << "_____" ;
cout << endl;
cout << endl;
for (int i=0; i< count; i++)
cout << name[i] << setw(30) << mark[i] << endl;
return 0;
}
;)
-
Like this -
cout << "Enter your mark : ";
cin >> mark[count];
while ((mark[count] < 0) || (mark[count] > 100))
{
cout << "PLs re-enter : " << endl;
cin >> mark[count];
}
sum += mark[count++];
-
Thanks a lot. Yes, the while loop is better as it can check until it is valid. And I made a mistake to put cin in the wrong place.
Thanks for help.
gogo