0

I am currently working on a personal project. For now, I am creating the part where it gets the users information. I am running into an issue where if the user chooses to correct their name, it steps over it and does not let the user re input the correct name. I have tried clearing before getting to the switch statement and in different locations.

Here is the switch statement:

switch (correction)
{
    case 1 : cout << "Name: ";
             cin.clear();
             getline(cin,name);
             checkInfo(age,number,name);
             break;
    
    case 2 : cout << "Age: ";
             cin >> age;
             while (age < 21)
             {
                cout << "Age is too low, try again." << endl;
                cin >> age;
             }
             checkInfo(age,number,name);
             break;
    
    case 3 : cout << "Number: ";
             cin >> number;
             while(!isNumeric(number) || number.size() < 8)
             {
                cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
                cout << "Number: ";
                cin >> number;
             }
             checkInfo(age,number,name);
             break;
    
    default : cout << "Please select options 1 through 3, nothing else is accepted: ";
              cin >> correction;
              break;
}

And here is where the user first in puts their information in the beginning:

cout << "Enter your name: ";
getline(cin,name);
cin.clear();
cout << "Enter your age: ";
cin >> age;
cin.clear();
cout << "Enter your phone number: ";
cin >> number;
cout << endl;

while (age >= 21)
{
    while (!isNumeric(number) || number.size() < 8)
    {
        cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
        cout << "Number: ";
        cin >> number;
    }

    checkInfo(age,number,name); 
}

}

I have a feeling i'm not clearing it somewhere correctly. Any help or recommendations to make this better is also appreciated. Thank you.

info.h file:

//info.h file
#ifndef INFO
#define INFO

#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>

using namespace std;

namespace info
{
    class user
    {
        public:
            
        char yesno;
        char confirm;
        int correction;
            
            
            void getInfo (int age, string number, string name);
            void checkInfo(int age, string number, string name);
            void changeInfo(int age, string number, string name);
            
        private:
        
            bool isNumeric(string str);
    };
}

void info::user::changeInfo(int age, string number, string name)
{
    cout << "Age: " << age << endl;
    cout << endl;
    cout << "Please select the number according to the information that needs to be changed." << endl;
    cout << endl;
    cout << "1. Name" << endl;
    cout << "2. Age" << endl;
    cout << "3. Number" << endl;
    cout << "Selection: ";
    cin >> correction;
    
    switch (correction)
    {
        case 1 : cout << "Name: ";
            cin.clear();
            getline(cin,name);
            checkInfo(age,number,name);
            break;
        
        case 2 : cout << "Age: ";
            cin >> age;
            while (age < 21)
                {
                    cout << "Age is too low, try again." << endl;
                    cin >> age;
                }
            checkInfo(age,number,name);
            break;
        
        case 3 : cout << "Number: ";
            cin >> number;
            while(!isNumeric(number) || number.size() < 8)
                {
                    cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
                    cout << "Number: ";
                    cin >> number;
                }
            checkInfo(age,number,name);
            break;
        
        default : cout << "Please select options 1 through 3, nothing else is accepted: ";
            cin >> correction;
            break;
    }
}

void info::user::getInfo (int age, string number, string name)
{   
    cout << "Enter your name: ";
    getline(cin,name);
    cin.clear();
    cout << "Enter your age: ";
    cin >> age;
    cin.clear();
    cout << "Enter your phone number: ";
    cin >> number;
    cout << endl;
    
    while (age >= 21)
        {
            while (!isNumeric(number) || number.size() < 8)
                {
                    cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
                    cout << "Number: ";
                    cin >> number;
                }
            checkInfo(age,number,name);
        }
}

void info::user::checkInfo(int age, string number, string name)
{   
    cout << "Is the given information correct?" << endl;
    cout << "-------------------" << endl;
    cout << endl;
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Number: " << number << endl;
    cout << endl;
    cout << "If yes, please press y for yes, n for no: ";
    cin >> confirm;
    
    while (age >= 21)
        {
            if (confirm == 'y' || confirm == 'Y')
                {
                    cout << "In my actual project, this is where i would save the information" << endl;
                    break;
                }
            else 
                {
                    changeInfo(age,number,name);
                    checkInfo(age,number,name);
                    break;
                }
        } 
}

bool info::user::isNumeric(string str) 
{
    for (int i = 0; i < str.length(); i++)
        {
            if(isdigit(str[i]) == false)
                {
                    return false;
                }
        }
    return true;
}

#endif

Main.cpp file:

    #include <iostream>
#include "info.h"

using namespace std;

int main ()
{
    int age;
    string number;
    string name;
    info::user userInfo;
    
    userInfo.final(age, number,name);

    return 0;
}
  • 1
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives, and the exact input required to reproduce the problem. Providing such an example allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel May 01 '22 at 17:58
  • 2
    Why are you calling `clear()` all the time? What do you think it does? (What you probably need to read is [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction.) – molbdnilo May 01 '22 at 18:04
  • that `while age` loop is never going to end. Why is it even there? – pm100 May 01 '22 at 18:07
  • @molbdnilo asked professor for some help on this, stated to use cin.clear(). Starting to think he's not good. – Edmunoz1030 May 01 '22 at 18:07
  • @pm100 It was just to get it to loop through. It leads to an infinite loop. Just figuring out piece by piece. – Edmunoz1030 May 01 '22 at 18:08
  • Did you try stepping through the code with a debugger? – Quimby May 01 '22 at 18:13
  • 2
    @Quimby yes i have. Never led me to my answer except show me that whenever the user tried to change the name, it would skip over it. cin.ignore() fixed my issue. – Edmunoz1030 May 01 '22 at 18:19

0 Answers0