Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a program that will prompt the user to enter a few details about him. I manage to separate the code by creating a class, then writing a code if they want to continue to input details again. The output seems to have a glitch idk about the Do While, after it takes the input of Y or y it won't let me type my name but instead, it proceeds to the next question.

What I have tried:

intMain.cpp:
C++
#include <iostream>
#include <limits>
#include "Classes.h"
using namespace std;

int main()
{
    asking();
    return 0;
}


Classes.h:
C++
#pragma once
#ifndef CLASSES_H_
#define CLASSES_H_


#define MAX_LENGTH 100
class Person {
public:
    char name[MAX_LENGTH] = { 0 };
    int age;
    int studentNo;
};

void asking();

#endif /*CLASSES_H_*/



Functions.cpp:
C++
#include "Classes.h"
#include <iostream>

using namespace std;

void asking() {
    char again;
	do {
        class Person p;
        p.name;
        p.age;
        p.studentNo;

        std::cout << "\nPlease Enter your name:";
        std::cin.getline(p.name, MAX_LENGTH);
        std::cout << "\nPlease Enter your age:";
        std::cin >> p.age;
        //input validation, allow only input integers
        while (1)
        {
            if (cin.fail()) {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                std::cout << "Error! Please enter only integers!\n";
                cin >> p.age;
            }
            if (!cin.fail())
                break;
        }

        std::cout << "\nPlease Enter your studentNumber:";
        std::cin >> p.studentNo;

        std::cout << "Your name is: " << p.name << endl;
        std::cout << "Your Age is: " << p.age << endl;
        std::cout << "Your student number is: " << p.studentNo << endl;
        std::cout << "\n";

        std::cout << "\tDo you want to input another data?(Y/N):";
        cin >> again;
    } while (toupper(again) == 'Y');
}
Posted
Updated 14-May-23 0:20am

1 solution

again is declared as a single char value, so the ENTER that terminated it is still in the input buffer.

I'd write a simple method:
C++
void EmptyBuffer()
   {
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }
And call it before each prompt:
C++
EmptyBuffer();
std::cout << "\nPlease Enter your name:";
std::cin.getline(p.name, MAX_LENGTH);
EmptyBuffer();
std::cout << "\nPlease Enter your age:";
std::cin >> p.age;
...
EmptyBuffer();
std::cout << "\tDo you want to input another data?(Y/N):";
cin >> again;
That way, anything left in the buffer is disposed of.
 
Share this answer
 
v2
Comments
ImMac4 14-May-23 22:52pm    
I tried your solution and it works. Thanks for the help :)
OriginalGriff 15-May-23 0:56am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900