1

I'm 3 weeks into CS50 and have spent a full day trying to get atoi to work. I'm getting the below error when compiling the below code.

My questions are:

  1. How do I represent each single character in the atoi() function?
  2. How do I only print the results only once? (the loop is making it print the number times of argv when I change atoi to argv[1])

Much thanks to the community in advance. I am eagerly to becoming a good programmer.

========= caesar.c:22:30: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion] int a = atoi(argv[1][i]); ^~~~~~~~~~

int main(int argc, string argv[])
{
    //if not exactly 1 argument, print error message a value of 1
    if (argc == 2)
    {
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (!isdigit(argv[1][i]))
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
            else
            {
                int a = atoi(argv[1][i]);
                printf("%i\n",a);
            }
        }
    }
    else
    {
        printf("Usage: ./caesar key\n");
    }
cs95
  • 379,657
  • 97
  • 704
  • 746
TPLFIRE
  • 21
  • 5

3 Answers3

0

simple use int a = argv[1][i] - '0'

explanation: We consider a single char in ASCII code which is a number between 0 and 255. The '0' '1' ... '9' is coincidentally/intentionally arranged in the same order of their value. In that case, we get the value of digital char by subtracting the ASCII number of '0'

anthony.hl
  • 73
  • 7
0

I suggest using a loop to check each digit of the string, then use a single call to atoi to convert the string to a number:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char *argv[])
  {
  //if not exactly 1 argument, print error message a value of 1

  if (argc == 2)
    {
    for (int i = 0; i < strlen(argv[1]); i++)
      {
      if (!isdigit(argv[1][i]))
        {
        printf("Usage: ./caesar key\n");
        return 1;
        }
      }

    int a = atoi(argv[1]);
    printf("%i\n", a);
    }
  else
    printf("Usage: ./caesar key\n");
  }

Example at onlinegdb here

  • Thank you. This worked. I did not change my "int main(int argc, string argv[])" and it still worked. May I ask why you changed it to char *argv[]), I'm not sure how it works and what is the difference? – TPLFIRE Nov 01 '20 at 03:53
  • I assume you have a header which defines a `string` type. From context it was clear that `string` == `char *`. I don't happen to have that header so I made the substitution. – Bob Jarvis - Слава Україні Nov 01 '20 at 15:45
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

bool check_valid (string s)
{
     for (int i = 0; i < strlen(s); i++)
     {
         if (isalpha(s[i]))
            return false;
     }
     return true;
}

int main (int argc, string argv[])
{
    if (argc != 2 || !check_valid(argv[1]))
    {
       printf("Usage: ./caesar key\n");
       return 1;
    }
}

`

sra js
  • 1
  • 1