Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve been working on this ONE function for about a day now. I no longer have ti

ID: 3670692 • Letter: I

Question

I've been working on this ONE function for about a day now. I no longer have time to try to figure this out alone as my project is due midnight tonight. The instructions seem vague, but from my understanding, a user inputs a number such as 334 and it returns true, but if they enter something like 33$4 which isn't an int, the function is supposed to return false. I understand that I am to go through the Cstring character by character using a for loop and checking with isdigit, but I'm not sure how to go about converting and initializing the cstring.

I'm not sure where to begin. Do i convert the int "value" to chars and then input them into the cstring? Seems a bit pointless. Do I convert the int to a string somehow? Wouldn't that mean it would only convert numbers and leave out alphabetical letters and symbols? Any help would be appreciated. Thanks!

bool getSeatNo(int& value) This function reads an 'int' amount into a C-string, verifies that all characters are digits. IF all characters are digits, they are converted to an int, that int is stored in value, and true is returned. Otherwise false is returned. You may find the and libraries helpful. Also, note that charDigit-'0' evaluates to intDigit (eg, '8,-'0' => 8) atchr se r evlits trne bes hepu, note

Explanation / Answer

#include <iostream>
#include<string>
#include <ctype.h>
using namespace std;


bool getSeatNo(int &value)
{   string s = "12345A";
   int i = 0;
   int n = s.size();
while (i<n) {
if (!isdigit(s.at(i)))
           return false;
       value = value*10 + (s[i]-'0');
       i++;
}

return true;;
}

int main (){

   int x;
   bool flag = getSeatNo(x);
   if(flag)
       cout<<x<<endl;
   else
       cout<<"String is not numeric ";
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote