Question

C++ language I am having some trouble with user validation, can someone look at my code...

C++ language

I am having some trouble with user validation, can someone look at my code and tell me what I am doing wrong:

char firstInital, lastInitial;
   int userAge;
   std::cout << "Program 1-2: Get user initials and age in days\n ";
   std::cout << "-------------------------------------------------------------------------\n";

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

   std::cout <<"Hello "<< firstInital << lastInitial << " Please enter your age in years: \n ";
   std::cin >> userAge;
   std::cout << firstInital << lastInitial << " You are " << userAge << " years old, which means that you are at least "
       << userAge * 365 << " days old!\n ";
   while (true)
   {
       if (cin >> userAge)
       {
           break;
       }
       else
       {
           cin.clear();
           cin.ignore(INT_MAX, '\n');
           std::cout << " Invalid input, please enter only positive integers and try again: \n ";
       }
      
   }

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! ";
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
C++ language I am having some trouble with user validation, can someone look at my code...
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
  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • Hello i am having a bit of trouble with a verified exit for my program. For...

    Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() {    char chChoice3 = ' ';    do    {        cout << "\n\t--Menu--" << endl;        cout << "1) Enter Player Name" <<...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide brief comments in the top code to show what is happening? THE CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;    }       for...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Professor Dolittle has asked some computer science students to write a program that will help him...

    Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points. At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the...

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
ADVERTISEMENT