Atoi() function in Challenge


#1

HI. First post, and let me start by saying what a great book so far!!

I’m after some help with regards to the atoi() function, which upon running the program duplicates the value that I enter in the console window. Type “2”, and “22” is displayed. Pressing backspace appears to produce an upside down question mark. The program does not produce any results, though there are no errors or warnings?

My OCD won’t let me carry on until I figure out what’s wrong? Here’s the entire main() function…

int main(int argc, const char * argv[])
{

printf("Where should we start from? ");

[b]const char *myStart = readline(NULL);[/b]

int theCounter = atoi(myStart);

for (int myCounter = theCounter; myCounter >= 0; myCounter--) {
    
    if (myCounter % 3 != 0) {
        
        continue;
        
    }
    
    printf("%d\n", myCounter);
    
    if (myCounter % 5 == 0) {
        
        printf("Found one!\n");
        
    }
    
}


return 0;

}

Thanks all.

Timbo!


#2

Hi Timbo,

It’s normal behaviour of XCode to type in one character and it reads back two (explained about 2 pages from the challenge).
I’ve reviewed your code (unsolicitedly) and it seems to me you can shorten it and make it more readable.
I added my solution at the bottom, for you to check if you like.

Check it out and let me know what you think :slight_smile:

[code]#include <stdio.h>
#include <readline/readline.h>

int main(int argc, const char * argv[]) {

    printf("Where should we start from? ");
    
    const char *myStart = readline(NULL);
    
    for (int myCounter = atoi(myStart); myCounter >= 0; myCounter--) {
        
        printf("%d\n", myCounter);
        
        if (myCounter % 5 == 0) {
            
            printf("Found one!\n");
            
        }
        
    }
    
    
    return 0;
    
} [/code]

My code:

[code]#include <stdio.h>
#include <readline/readline.h>

int main(int argc, const char * argv[]) {

printf("Where would you like to start? ");
const char *name = readline(NULL); {
    for (int i = atoi(name); i >= 0; i -= 3) {
        printf("%d\n", i);
        if (i % 5 == 0) {
            printf("Found one!\n");
        }
    }
}return 0;

}[/code]


#3

There is no need for any of these functions. Think simple.

My code

[code]#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, const char * argv[])
{
int x = 99;
while (x >= 0) {
printf("%d\n", x);
if (x % 5 == 0)
printf(“Found one!\n”);
x -= 3;

}
return 0;
}
[/code]
Don’t complicate your code unless you have too. I guess you’ve done some programming before as the atoi function isn’t covered so far. You could use it if you wanted practice but I don’t see any other reason not to keep it simple.


#4

[quote]x = x -= 3; [/quote]
reduces to:

x -= 3;

I know you wrote it like that by accident. :slight_smile:


#5

[quote=“ibex10”][quote]x = x -= 3; [/quote]
reduces to:

x -= 3;

I know you wrote it like that by accident. :slight_smile:[/quote]
You’re absolutely right. I will edit my post and my code.
Thanks!