Using atoi to get inputs after prompting the user?

Mar 31, 2014 at 5:36am
Hello! I'm really new to using atoi and I've only really used it as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char** argv){

	float r = atoi(argv[1]);
	float i = atoi(argv[2]);

	cout << "The first input is " << r << " and the second is " << i << endl;

}


I compile it and then run it with: ./"test" 2 5
and that would output "The first input is 2 and the second is 5"


If I wanted to have a string display, say, something like "Please enter the first number: " and THEN have the user input a number, how would I go about doing that using atoi?

I've read: http://www.cplusplus.com/reference/cstdlib/atoi/
I'm trying to stay away from using printf since we aren't allowed to use it in my programming methodology class.
Last edited on Mar 31, 2014 at 5:36am
Mar 31, 2014 at 5:59am
You are not using printf for output. You are using std::cout. That should already cover the "how to display".

printf and std::cout are for output. There is a corresponding input stream too. It has operator >> that writes data directly to numeric type. No need for atoi().

PS. atoi() creates an integer. You store to float. Why?


[edit] Please, no doubleposting.
Last edited on Mar 31, 2014 at 6:05am
Topic archived. No new replies allowed.