Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:19:21+00:00 2026-06-11T18:19:21+00:00

I am reading input for my program in a loop using getline. string temp(STR_SIZE,

  • 0

I am reading input for my program in a loop using getline.

    string temp(STR_SIZE, ' ');
    string str_num(STR_SIZE, ' ');
    ...
    getline(cin, temp, '\n');

After which, I use a function to find the next delimiter(white space) and assign all the characters before the white space to str_num. Looks something like this:

    str_num.assign(temp, 0, next_white_space(0));

I have verified that this works well. The next step in my solution would be to convert str_num to an int(this part also works well), but I should check to make sure each character in str_num is a digit. Here’s the best of what I’ve tried:

    if(!isdigit(str_num[0] - '0')) {
        cout << "Error: Not an appropriate value\n";
        break; /* Leave control structure */
    }

For some reason, This always prints the error message and exits the structure.
Why is that?
I’ve used operator[] for string objects before, and it seemed to work well. But, here, it’s totally messing me up.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:19:23+00:00Added an answer on June 11, 2026 at 6:19 pm

    std::isdigit takes a char’s integer value and checks it.

    So, remove the – ‘0’ and just pass str_num[index] to isdigit().

    Note: because this function comes from C, the old style of treating chars as integers shows through in the method taking an int. However, chars can promote to int values, so a char becomes an int just fine and this works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The program takes a string using getline, and then passes that string to a
I'm reading input in a C++ program. First some integers, then a string. When
I'm using a abstract class in another module for reading and input for my
I am writing a simple software protection tool which works by reading the input
I'm reading from the standard input using the read() system call but there's a
I'm reading some third party Verilog, and found this: function [31:0] factorial; input [3:0]
So I'm reading in a file (like java program < trace.dat) which looks something
(Scanner Input=new Scanner(System.in)) I have a while loop in my program with Input.hasNext() condition.
In a program I'm reading in some data files, part of which are formatted
I am using a regex to program an input validator for a text box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.