My program compiles, but does not run

This is a discussion on My program compiles, but does not run within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program that basically inputs info into an array, and then outputs it again in something ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    Near Corruption
    Posts
    2

    My program compiles, but does not run

    I'm trying to write a program that basically inputs info into an array, and then outputs it again in something approaching a table. Like the topic title suggests, it compiles, but doesn't run, instead shutting down with the windows error: x has encountered an error and needs to close.... I have no idea why it's shutting down. Could someone at least point me in the direction of what's going wrong?

    I'm using MSVC++ Exp. V. 8.0 to compile it.

    Here's the source:
    Code:
    //EOC Exercise 6.1 - Grading students
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cctype>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::setw;
    
    int main() {
    	
    	const int max_number_of_students = 100;
    	char control = 0;
    	int index = 0;
    
    	string students[max_number_of_students] = {0};
    	int grades[max_number_of_students] = {0};
    
    	do {		
    		cout << "Enter the name of the student: ";
    		cin >> students[index];
    
    		cout << "Enter the students grade: ";
    		cin >> grades[index];
    
    		index++;
    		
    		cout << "Do you need to enter another grade? (y/n)";
    		cin >> control;
    
    	} while((index <= max_number_of_students) | (std::tolower(control)) != 'y');
    
    	if(index >= max_number_of_students)
    		cout << "\nMaximum reached!";
    
    	float average = 0;
    
    	for(int i_avg = 0; i_avg <= index; i_avg++)
    		average += static_cast<float>(grades[i_avg]);
    
    	cout << endl << "The average grade is: " << (average / static_cast<float>(index)) << " points.";
    
    	for(int table_index = 0; table_index <= index; table_index++){
    		cout << setw(10) << students[table_index] << ": "
    			<< setw(5) << grades[table_index];
    	}
    
    	return 0;
    }
    I'm trying to teach myself C++. I've got a book and stuff (Ivor Horton's:Beginning C++, if anyone is concerned), and I think I'm making progress, so excuse me if I sound like an idiot.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > while((index <= max_number_of_students) | (std::tolower(control)) != 'y')
    You probably want the || boolean test, not the bitwise |

    > for(int i_avg = 0; i_avg <= index; i_avg++)
    Both these loops should be < not <=
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Is tolower() part of std:: ?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        string students[max_number_of_students] = {0};
    you are trying to initialize a string with an int.
    The default constructor for string initializes it to "".
    Try
    Code:
        string students[max_number_of_students];
    Kurt

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    Near Corruption
    Posts
    2
    Quote Originally Posted by ZuK
    Code:
        string students[max_number_of_students] = {0};
    you are trying to initialize a string with an int.
    The default constructor for string initializes it to "".
    Try
    Code:
        string students[max_number_of_students];
    Kurt

    That was it, thanks.

    A few more mistakes (like the boolean one, which should've been && anyway) and it works like I wanted it too.

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 02:01 AM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  4. Make a program run at startup
    By cookie in forum Tech Board
    Replies: 2
    Last Post: 06-08-2007, 08:36 PM
  5. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM

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