2

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.

7
  • is this C or C++? If C, what's string? Commented Nov 24, 2012 at 18:14
  • It's c++ (<string> is included) Commented Nov 24, 2012 at 18:15
  • The question is rather: why did you tag this C if it's C++? Commented Nov 24, 2012 at 18:16
  • 1
    scanf("%s"...); expects the argument to be a char buffer. Whereas string is a class. Commented Nov 24, 2012 at 18:17
  • 1
    1. That's not necessarily true (did you benchmark it?). 2. You don't have big inputs. 3. You don't know how to use scanf, so it won't help you anyway. Commented Nov 24, 2012 at 18:25

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

That's a really bad way to check for end-of-input. Either use an integer or use a string.

If you choose string, make provisions to convert from string to int.

My logic would be to first check if it can be converted to integer. if it can be, then continue with the logic. If it can't be(such as if it's a float or double or some other string) then ignore and move on. If it can be, then insert it into Halda's object.

Sidenote: Do not use scanf() and printf() when you're working with C++.

Comments

0

Assuming string refers to std::sring this program doesn't have defined behavior. You can't really use std::string with sscanf() You could set up a buffer inside the std::string and read into that but the string wouldn't change its size. You are probably better off using streams with std::string (well, in my opinion you are always better off using streams).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.