1

The program I am working is passed 3 command line arguments, all of which should be integers. the skeleton program that was provided has:

int main(char *argv[]){
...
}

What I have done the provided is just try to set an integer to the item in each position of the array (0, 1, & 2), but it won't compile since its trying to convert a char to an integer. I'm sure there is an easy fix for this, but I can't seem to figure it out.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bigby
  • 193
  • 2
  • 4
  • 12
  • take a look at this post: [link](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – neu-rah May 08 '12 at 14:29

3 Answers3

5

Since this looks like homework, I'll give you several of hints:

  • Signature of main() includes an int argument, conventionally named argc
  • The initial argument is argv[1], not argv[0]
  • atoi is the easiest way to convert a string to an integer.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Won't downvote but is `atoi` really what a **C++** beginner is looking for ? – cnicutar May 08 '12 at 14:31
  • 1
    @cnicutar If I were teaching a beginners course in C++, that would be one of the first things I'd quickly mentioned before moving on to advanced I/O topics with string streams etc. `atoi` is a first-class citizen among other functions of the standard C++ library, I think it is worth mentioning. – Sergey Kalinichenko May 08 '12 at 14:33
  • 2
    If I knew C++ I would never mention `cstdlib` functions to beginners. And `atoi` is always a second-class citizen (**even in C**) because it's unsafe. – cnicutar May 08 '12 at 14:34
  • @cnicutar I prefer the "simple tools for simple tasks" approach. I start with a `char*`, and there is a function that interprets `cons char*` as a decimal integer. My problem is solved! Now, had I started with `std::string`, I'd definitely stay away from `atoi(s.c_str())`: once you're in C++ land, it's not a good idea to cross back. – Sergey Kalinichenko May 08 '12 at 14:41
  • @cnicutar I am reasonably certain that `atoi` is safe, at least its C++ cousin: it does not overflow on larger-than-INT_MAX. "If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned." – Sergey Kalinichenko May 08 '12 at 14:43
  • Need to be careful though because atoi returns a perfectly valid and perfectly common integer(zero) on error. So if that happens, you need to parse the string by hand to see if it's actually zero. – Benjamin Lindley May 08 '12 at 14:51
  • so would using something like **int x = atoi(argv[1])** work? – Bigby May 08 '12 at 14:58
  • @BigBrown Yes, that would get you the first int. atoi(argv[2]) will get the second, and so on. You need to check argc to be four or more in order to not go past the end of the parameter array passed in from the operating environment. – Sergey Kalinichenko May 08 '12 at 15:04
0

Try something like this:

int i = atoi(argv[1]);
Roman Bataev
  • 9,287
  • 2
  • 21
  • 15
0

Try the cstdlib function "atoi" for each argument:

char* to int conversion

tmaric
  • 5,347
  • 4
  • 42
  • 75