Looping help

the first run is fine. But on the second run, it skips the "Student_Name". What is wrong with the code?



#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main()
{



char student_name[50];
char book_name[50];
char choice;
string student_number;
int receive;
int change;
int time;
int total;
int price = 40;
int ctr1;

do{
system("CLS");

cout << "\t\t\tSTUDENTS' LENDING BOOK SYSTEM\n\n";

cout << "Please enter the student's name: ";
cin.getline(student_name,50);

cout << "Please enter the student number: ";
getline(cin,student_number);

cout << "Please enter the title of the book: ";
cin.getline(book_name,50);

cout << "hours of borrowing book: ";
cin >> time;

if (time >= 1 && time <= 12)
{
total = price;

cout << "Amount Received: ";
cin >> receive;


cout << endl << endl;

cout << "_____________________________________________________";

cout << endl << endl;


cout << "Student's Name: ";

for(ctr1 = 0 ; student_name[ctr1] != '\0' ; ctr1++)
{
cout << student_name[ctr1];
}

cout << endl << endl;

cout << "Student number: " << student_number;

cout << endl << endl;

cout << "The title of the book is: ";

for(ctr1 = 0 ; book_name[ctr1] != '\0' ; ctr1++)
{
cout << book_name[ctr1];
}

cout << endl << endl;

cout << "Borrowing hours: " << time << "Hours";

cout << endl << endl;

cout << "Total amount: " << total << " Pesos";

cout << endl << endl;

cout << "Amount Received: " << receive << " Pesos";

cout << endl << endl;

cout << "Change: " << receive - total << " Pesos";

cout << endl << endl;
}


else if (time >=13 && time <= 24)
{
total = price*2;

cout << "Amount Received: ";
cin >> receive;



cout << endl << endl;

cout << "_____________________________________________________";

cout << endl << endl;


cout << "Student's Name: ";

for(ctr1 = 0 ; student_name[ctr1] != '\0' ; ctr1++)
{
cout << student_name[ctr1];
}

cout << endl << endl;

cout << "Student number: " << student_number;

cout << endl << endl;

cout << "The title of the book is: ";

for(ctr1 = 0 ; book_name[ctr1] != '\0' ; ctr1++)
{
cout << book_name[ctr1];
}

cout << endl << endl;

cout << "Borrowing hours: " << time << " Hours";

cout << endl << endl;

cout << "Total amount: " << total << " Pesos";

cout << endl << endl;

cout << "Amount Received: " << receive << " Pesos";

cout << endl << endl;

cout << "Change: " << receive - total << " Pesos";

cout << endl << endl;
}


else if (time > 24 && time <= 500)
{
total = ((time - 24)*25)+(price*2);

cout << "Amount Received: ";
cin >> receive;



cout << endl << endl;

cout << "_____________________________________________________";

cout << endl << endl;


cout << "Student's Name: ";

for(ctr1 = 0 ; student_name[ctr1] != '\0' ; ctr1++)
{
cout << student_name[ctr1];
}

cout << endl << endl;

cout << "Student number: " << student_number;

cout << endl << endl;

cout << "The title of the book is: ";

for(ctr1 = 0 ; book_name[ctr1] != '\0' ; ctr1++)
{
cout << book_name[ctr1];
}

cout << endl << endl;

cout << "Borrowing hours: " << time << " Hours";

cout << endl << endl;

cout << "Total amount: " << total << " Pesos";

cout << endl << endl;

cout << "Amount Received: " << receive << " Pesos";

cout << endl << endl;

cout << "Change: " << receive - total << " Pesos";

cout << endl << endl;

}
else
{
cout << "Invalid input of amount!!";
cout << endl << endl;
cout << endl << endl;
}

cout << endl << endl << endl;

cout << "Do you still want to add another one?(y or n)";
cin >> choice;


}while(choice == 'Y' || choice == 'y');

system("pause");
return 0;

}
The likely culprit is an endline or other character left over in the input buffer. You can use cin.ignore() to clear the buffer between inputs.
It happens sometimes when you use getline() and cin together in a program.
There are 2 solutions to this.

1) You can use the getline function to get the 'time' and the 'amount' and use the stoi (string to int conversion) function to convert the string to int.
For ex:
1
2
3
4
string StrNum;
getline(cin, StrNum);
int Inum;
Inum = stoi(StrNum); 


2) Another solution is to use cin.ignore() after you cin amount received.

Solution 2 is not always helpful so try solution 1 if possible.

Hope this helps!
Last edited on
Topic archived. No new replies allowed.