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..