Thread: Infinate loop!!!

  1. #1

    Angry Infinate loop!!!

    I created a function in my program that would take info about a teacher and their class room and then write it to a file. Then I added a loop so that people could keep adding more teachers. Then I thought I would be good to make a break statement so people can easily close the program. The problem is, is that after you cycle through this once it starts looping and never stops for user input. Here's the code-

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int teacher_info(char name[10], char subject[10], int room_num)
    {
    	ofstream fout("school.txt");
    	fout << name << ":\n\tSubject: " << subject << "\n\tRoom number: " << room_num << endl;
    	fout.close();
    	return 0;
    }
    
    int main()
    {
    	char name[10], subject[10];
    	int room_num;
    	
    	cout << "\nNOTE: to exit type 'exit' as the teacher name." << endl;
    	
    	while (true)
    	{
    		cout << "\nName: ";
    		cin >> name;
    		if (name == "exit")
    			break;
    		cout << "\nSubject: ";
    		cin >> subject;
    		cout << "\nRoom number: ";
    		cin >> room_num;
    		teacher_info(name, subject, room_num);
    	}
    	return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char name[10], subject[10];
    These are too short to be of any real use. They only allow for 9 characters to be entered.

    >>if (name == "exit")
    You cannot compare char arrays like this, you need strcmp().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Classic misstake
    Code:
    if (name == "exit")
    try
    Code:
    if (std::strcmp(name, "exit") == 0)

  4. #4
    Thanks that worked. And I had though 9 bytes would be enough for a name. But I will increase it to.... lets say... 30?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I will increase it to.... lets say... 30?
    Whatever you see fit. Or, change to using strings instead of char arrays, that would be the C++ thing to do

    Also, this:
    cin >> name;
    will get into trouble if someones name contains 2 words, for example enter "john smith" on the name prompt and see what happens.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    I didnt think about people using spaces. Like Mrs. Johnson. So then I will just use string so both problems are solved.

  7. #7
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    With char arrays, you could use gets(name) and any whitespace will be included...
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by abrege
    With char arrays, you could use gets(name) and any whitespace will be included...
    gets() is very bad to use, it is an old C function that should be avoided. It does no buffer length checking, so you are building in potential hazards into your code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Originally posted by abrege
    With char arrays, you could use gets(name) and any whitespace will be included...
    Why not getline()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. Infinate Loop, not for all
    By Erf in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2004, 02:26 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM