run time error?

This is a discussion on run time error? within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote the following program which can be compiled and built in Visual C++ compiler. However, it returned an ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Unhappy 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;

    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    2
    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.

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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.
    zen

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    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;

    }





  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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++];
    zen

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21