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 8884245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:01:10+00:00 2026-06-14T21:01:10+00:00

I need to read input from user. The input value may be string type

  • 0

I need to read input from user. The input value may be string type or int type.
If the value is int then the program insert the value into my object.
Else if the value is string then it should check the value of that string, if it’s “end” then the program ends.

Halda h; //my object
string t;
int tint;
bool end=false;

while(end!=true)
{
    if(scanf("%d",&tint)==1)
    {
        h.insert(tint);
    }
    else if(scanf("%s",t)==1)
    {
      if(t=="end")
          end=true;
      else if(t=="next")
          if(h.empty()==false) 
              printf("%d\n",h.pop());
          else 
              printf("-1\n");
    }
}

The problem is that scanning string doesn’t seem to work properly.
I’ve tried to change it to: if(cin>>t) and it worked well.
I need to get it work with scanf.

  • 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-14T21:01:10+00:00Added an answer on June 14, 2026 at 9:01 pm

    The specifier %s in the scanf() format expects a char*, not a std::string.

    From C11 Standard (C++ Standard refers to it about the C standard library):

    Except in the case of a % specifier, the input item (or, in the case of a %n directive, the
    count of input characters) is converted to a type appropriate to the conversion specifier. If
    the input item is not a matching sequence, the execution of the directive fails: this
    condition is a matching failure. Unless assignment suppression was indicated by a *, the
    result of the conversion is placed in the object pointed to by the first argument following
    the format argument that has not already received a conversion result. If this object
    does not have an appropriate type, or if the result of the conversion cannot be represented
    in the object, the behavior is undefined
    .

    Anyway, here there’s is no real reason to prefer the C way, use C++ facilities. And when you use the C library, use safe functions that only reads characters up to a given limit (just like fgets, or scanf with a width specifier), otherwise you could have overflow, that leads again to undefined behavior, and some errors if you’re luck.

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

Sidebar

Related Questions

I want to read input from user using C program. I don't want to
I'm writting a Ruby program for windows, and I need to read user input
I need to read some data from an input file and plot a graph
I need to read 16 bits from the binary file as std::string or char
trying to read text into memory from a text file the user selects MODEL:
I need to read any line (from user_input) into an atomic list, e.g.: Example
I need to read the serial port as an input for a web based
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I need to read certain statistics from iw_statistics structure, here's the code: struct net_device
I need to read data from XML to a List<>. The XML file contains

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.