-
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::out);
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;
}
}
-
I think that you should ask programming master chris, he knows all.
-
Chris knows nothing, I know everything!!
-
Chris knows EVERYTHING!!!!
-
hey, I'm chris! be nice! It looks like if you choose 4 it should exit the loop. is that not the case?
-
I tested! when i enter 4 it exist the loop! so where is the problem?
-
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.. ;)
-
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.
-
but how can you be chris if you're bob?
-
-
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.
-
>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.