0

Per the pset2 specifications, I am using

string k = argv[1];

int k = atoi(argv[1]);

to turn the input from a string to an int but when I try to compile I get:

caesar.c:16:5: error: redefinition of 'k' with a different type: 'int' vs
      'string' (aka 'char *')
int k = atoi(argv[1]);

caesar.c:15:8: note: previous definition is here
string k = argv[1];

which is crazy because I am trying to redefine the variable. Any ideas on how to fix would be helpful.

2

You have two local variables with the same name (i.e., k) within the same scope and that's not correct! This actually makes the compiler get confused because it doesn't know which k of them you are referring to when using the name of the variable (i.e., k).

The solution is simply to assign each of the string and the int variables a different name. For example

string s = "123";
int i = atoi(s);

It's a best practice to create variables with descriptive names!

Update: you actually don't need to create another string variable to store the value of argv[1] which is already a string. You may use argv[1] directly!

2
  • Ok but if k is a string how do I turn it into an int without redefining it? – Sofia Jun 25 '14 at 19:46
  • @user1353 I just updated my answer, you may reread it! :) – Kareem Jun 25 '14 at 19:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .