Question

c++ language I am trying to practice user validation, as in if I enter a letter...

c++ language

I am trying to practice user validation, as in if I enter a letter instead of a number, I get a 0 for the output, can someone look at my code and tell me how to fix it

char firstInital, lastInitial;
   int userAge;
   std::cout << "Program 1-2: Get user initials and age in days\n ";
   std::cout << "-----------------------------------------------\n\n ";
       std::cout << "Please enter the first letter of your first name: ";
       std::cin >> firstInital;
       std::cout << "Please enter the first letter of your last name: ";
       std::cin >> lastInitial;

       std::cout << " Hello " << firstInital << " " << lastInitial << " Please enter your age in years: ";
       std::cin >> userAge;
       while (!(cin >> userAge))
       {
           cin.clear();
           while (cin.get() != '\n') continue;
           std::cout << " Error, age must be a positive integer only, please try again: ";

       }
       std::cout << firstInital << " " << lastInitial << " You are " << userAge << " years old, which means you are at least "
           << userAge * 365 << " days old!";
  
   std::cout << "Press any key to exit." << endl;

2nd part:

int myArray[5];
   for (int i = 0; i < 5; i++)
   {
       std::cout << "Please enter a number for the array: ";
       std::cin >> myArray[i];
       while (!(cin >> myArray[i]))
       {
           std::cout << " Invalid entry, please try again: ";
           cin.clear();
           cin.ignore(100, '\n');
       }
   }
   for (int i = 0; i < 5; i++)
       std::cout << myArray[i] << " ";

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey,

Below is the edited code to your question

#include<iostream>
#include <sstream>
using namespace std;
int check(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]<48||s[i]>57)
return 0;
}
return 1;
}
int main()
{
char firstInital, lastInitial;
int userAge;
string age;
std::cout << "Program 1-2: Get user initials and age in days ";
std::cout << "------------------------------------------------------------------------- ";

std::cout << "Please enter the first letter of your first name: " << flush;
std::cin >> firstInital;
std::cout << "Please enter the first letter of your last name: ";
std::cin >> lastInitial;

std::cout <<"Hello "<< firstInital << lastInitial << " Please enter your age in years: ";
std::cin >> age;

while (check(age)==0)
{


std::cout << " Invalid input, please enter only positive integers and try again: ";
cin >> age;


}
stringstream g(age);
g>>userAge;

std::cout << firstInital << lastInitial << " You are " << userAge << " years old, which means that you are at least "
<< userAge * 365 << " days old! ";
}

2)

#include<iostream>
#include <sstream>
using namespace std;
int check(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]<48||s[i]>57)
return 0;
}
return 1;
}
int main()
{
int myArray[5];
for (int i = 0; i < 5; i++)
{
std::cout << "Please enter a number for the array: ";
string num;
cin>>num;
while (check(num)==0)
{


std::cout << " Invalid input, please enter only integers and try again: ";
cin >> num;


}
stringstream g(num);
g>>myArray[i];
}
for (int i = 0; i < 5; i++)
std::cout << myArray[i] << " ";
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
c++ language I am trying to practice user validation, as in if I enter a letter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT