infinite loop

This is a discussion on infinite loop within the C++ Programming forums, part of the General Programming Boards category; I have an infinite loop in a program that I can't resolve. The loop doesn't always take place. Sometimes it ...

  1. #1
    Banned dukemarlon's Avatar
    Join Date
    Nov 2002
    Posts
    14

    Question infinite loop

    I have an infinite loop in a program that I can't resolve. The loop doesn't always take place. Sometimes it doesn't. I need Help. Review me code and reply please. Thanks. P.S. Don't laugh at my code.

    #include<iostream>
    #include<fstream>
    #include<string>
    #include<iomanip>
    using namespace std;
    void menu();
    void bubble_sort();
    char name[30], file[25];
    int choice, number;

    main()
    {
    cout<<"Please enter the name of the file you wish to work with.\n(Remember the extension!)\n";
    cin.get(file, 25);
    cin.ignore(80, '\n');
    ofstream outfile;
    outfile.open(file, ios:ut);
    if(file)
    {
    do
    {
    menu();
    }while(choice!=4);
    }
    else
    {
    cout<<"There was an error handling the requested file.\n";
    }

    outfile.close();
    return 0;
    }
    void menu()
    {
    cout<<"What would you like to do?\n1. Insert a persons name and phone number\n2. Sort with Bubble Sort\n3. Sort with \n4. Exit\n";
    cin>>choice;
    switch(choice)
    {
    case 1:
    cout<<"Please enter the person's name then their phone number (numbers only, no spaces).\n";
    cin.get(name[30]);
    cin.ignore(80, '\n');
    cin>>number;
    }
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    126
    I think that you should ask programming master chris, he knows all.

  3. #3
    Banned dukemarlon's Avatar
    Join Date
    Nov 2002
    Posts
    14
    Chris knows nothing, I know everything!!

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    126
    Chris knows EVERYTHING!!!!

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    hey, I'm chris! be nice! It looks like if you choose 4 it should exit the loop. is that not the case?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    I tested! when i enter 4 it exist the loop! so where is the problem?

  7. #7
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    i'm no expert, and maybe shouldn't be the one doing this, but i'll give it a shot...

    Code:
    cout<<"Please enter the person's name then their phone number (numbers only, no spaces).\n";
    cin.get(name[30]);
    cin.ignore(80, '\n');
    cin>>number;
    this (i think) tries to put the first character the user inputs in to name[30], which is a single char that doesn't actually exist (because you only have name[0] through name[29])

    the only time i could get an infinite loop was when i hit enter without inputting anything.i think this has to do with mixing cin.get() and cin>> (one leaves spaces and '\n's in the stream, or something...)

    try...

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<iomanip>
    using namespace std;
    void menu();
    void bubble_sort();
    char name[30], file[25], 
    		input[80];	//to hold input before converting to int
    int choice, number;
    
    main()
    {
    	cout<<"Please enter the name of the file you wish to work with.\n(Remember the extension!)\n";
    	cin.get(file, 25);
    	cin.ignore(80, '\n');
    	ofstream outfile;
    	outfile.open(file, ios:ut);
    	if(file)
    	{
    		do
    		{
    			menu();
    		}while(choice!=4);
    	}
    
    	else
    	{
    		cout<<"There was an error handling the requested file.\n";
    	}
    
    	outfile.close();
    	return 0;
    }
    
    void menu()
    {
    	cout<<"What would you like to do?\n"
    		<<1. Insert a persons name and phone number\n"
    		<<2. Sort with Bubble Sort\n3. Sort with \n4. Exit\n";
    	cin.getline(input, 80, '\n');	//get input
    	choice = atoi(input);			//convert to int
    	switch(choice)
    	{
    		case 1:
    		cout<<"Please enter the person's name then their phone number (numbers only, no spaces).\n";
    		cin.getline(name, 30, '\n');	//get name
    		cin.getline(input, 80, '\n');	//get number
    		number = atoi(input);			//convert number to int
    	}
    }
    when i ran this, hitting enter without inputting anything only took me back to the menu (no infinite loop).

    i'm far from an expert at this, so if someone knows of a better way to do this, please step in.

    sorry for the long drawn out reply... i still need to work on this whole "explaining" thing..

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    126
    Chris is and always will be the best. In regards to it exiting the program when 4 is entered, it does most of the time, but once in a while it doesn't. I know this because I am Chris.

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    but how can you be chris if you're bob?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    126
    MAGIC!!!

  11. #11
    Registered User adamviper's Avatar
    Join Date
    Nov 2002
    Posts
    132
    when in doubt read the green book and the reason bob is chris is beacuse of PRINTF and also 12 those are the answers to everything in life.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    126
    >when in doubt read the green book and the reason bob is chris is beacuse of PRINTF and also 12 those are the answers to everything in life.

    I agree, most definatly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

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