2

In C, suppose I need to take input from a string

 int num,cost;
 char *name[10];
 printf("Enter your  inputs [quantity item_of_name at cost]");
 scanf("%d%*c%s%*c%*s%*c%d",&num,name[0],&cost);

 printf("quantity of item: %d",num);
 printf("the cost of item is: %d",cost);
 printf("the name of item is: %d",name[0]);

INPUT

1 book at 12

OUTPUT

Quantity of item is: 1

The cost of item is: 12

The name of item is: book

Now I want to do the same thing in C++. And I have no idea how to approach. gets() returns the whole string.Is there any specific function that I am missing out on? Please help.

4
  • Your question is a bit confused because your C code doesn't take input from a string but from standard input. What are you really trying to do? Also the C functions you are using will also work in C++. Commented Oct 12, 2012 at 11:01
  • 1
    His C code is broken anyway: he passes an uninitialized pointer as the third argument to scanf. And even if it were initialized: users can easily crash the program with a long enough name. In C, you never use "%s" in a scanf without specifying the maximum length. Commented Oct 12, 2012 at 11:10
  • All I am trying is making use of format specifiers to store the appropriate input (in the string )into corresponding variables. Now I have never seen usage of C functions (or what we use in C) in C++. (I am not saying you can't..just I am still a beginner).Basically I am trying to transpose the problem from C to C++. Commented Oct 12, 2012 at 11:10
  • @Peps The C++ way of doing input is given by sehe, but the C way works too. Commented Oct 12, 2012 at 11:20

4 Answers 4

6
int num,cost;
std::string name;
std::cout << "Enter your  inputs [quantity item_of_name at cost]: ";
if (std::cin >> num >> name >> cost)
{ } else 
{ /* error */ }

You will want to add errorhandling

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

Comments

0

In C++ you should rather use cin, cout and string from standard library.

Comments

0

In c++, std::stream provides the read and write communication with the user through the >> operator.

Your code translates to

int num,cost;
std::string name;

std::cout << "Enter your  inputs [quantity item_of_name at cost]" << std::flush;
std::cin >> num >> name;
std::cin >> at; // skip the at word
std::cin >> cost;

std::cout << "quantity of item: " << num << std::endl;
std::cout << "the cost of item is: " << cost << std::endl;
std::cout << "the name of item is: " << name << std::endl;

Comments

0

You could use iostream's cin.

int num,cost;
 char *name[10];
 std::cout <<"Enter your quantity"<<std::endl;
 std::cin>> num;
 std::cout<<" Enter the cost"<<std::endl;
 std::cin>>cost;
 std::cout<<"Enter the name"<<std::endl;

 std::cout<<"The quantity of the item is: "<<num<<" costing: "<<cost<<" for "<<name[0]<<std::endl;

and then of course you could also use std::string instead of char*.

Or streamline the cin's as cin >> num >> cost >> name;

Also, as Griwes noted, you will want to perform error checking on your results.

4 Comments

Yeah, but I would imagine that goes without saying.
OP looks like newbie (generally, he looks like someone educated that C++ is just "C with classes" and taught some C basics), so I think it's important remark here.
My assumption is that because he was using (well knew how to) a array of char pointers, that he knew error handling, but in my sedation didn't stop to consider this could be a homework question and that code just have been provided. Good catch, and noted as well.
Your code yields the same undefined behavior as the questioner's. Better replace could with should and tell him/her about the error.

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.