Question
I am having issues with my void addRecords function. When asked to enter the salesperson's name, if I give a first name based input then
I am having issues with my void addRecords function. When asked to enter the salesperson's name, if I give a first name based input then it will work (if I have it to cin >> salesName). However, I must enter the full name, first and name. It works the first time the program is ran, however the second time it does not it gives me a infinite loop, skipping the 2nd line inside the while loop.
#include
using namespace std;
char getChoice(); char choice; void addRecords(); void displayTotal();
int main() {
do { choice = getChoice(); if (choice == '1') { addRecords(); } else if (choice == '2') { displayTotal(); } else { cout << "Good Bye"; return 0; }
} while (choice != 3);
system("pause"); } //end of main function
char getChoice() { char menuChoice = '0'; cout << endl << "Menu Options" << endl; cout << "1 Add Records" << endl; cout << "2 Display Total Sales" << endl; cout << "3 Exit" << endl; cout << "Choice (1, 2, or 3)? "; cin >> menuChoice; cin.ignore(100, ' '); cout << endl; return menuChoice;
}
void addRecords() { char salesName[25]; double salesAmount; ofstream fout; fout.open("C:\\problems\\sales.txt");
if (fout.is_open()) { cout << "Salesperson's name(X to quit): "; //cin.getline(salesName, 25); // Used this for it to be a full name based input, but after the first while loop has been ran, I type a second full name then after that everything inside the while loop gets skipped and turns into a infinite loop. cin >> salesName;
while (salesName[0] != 'X' && salesName[0] != 'x') {
cout << "Saleperson amount: "; cin >> salesAmount;
fout << salesName; fout << salesAmount; cout << "Salesperson name (X to quit): "; cin >> salesName;
} fout.close(); } else { cout << "sales.txt file could not be opened."; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started