Countdown Challenge atoi difficulty


#1

Had a lot of trouble with this one. I ended up wasting a couple of hours just because I didn’t use the #include <stdlib.h> at the top of main.c file.
Not sure if anyone else has had this issue or if my code is the most efficient for that matter. Any input or constructive criticism is always appreciated.

//
//  main.c
//  CountDown
//
//  Created by Thomas Eggenberger on 12/30/13.
//  Copyright (c) 2013 Big Nerd Ranch. All rights reserved.
//
#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>//needed to use the atoi function

int main(int argc, const char * argv[])
{
    printf("Where should I start counting?");
    const char *num = readline(NULL);
    int count = atoi(num);
    
    for (int i = count; i >= 0; i -= 3){
        printf("%d\n", i);
        if (i % 5 == 0) {
            printf("Found one!\n");
        }
    }
    return 0;
}

#2

Did you try the case with count starting with value 0?

For example, what would happen if you entered a non-numeric input such as TripleJ?

Also, if you post your code between the Code tags you will make it easier for others to view your code:

...
const char *num = readline (NULL);
int count = atoi (num);
...

#3

Ahh… Yes great idea. That does indeed look much better and I will try entering a non int value when I get back to my computer.


#4

here’s how i did it, in case you want to see another way

#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>//needed to use the atoi function

int main(int argc, const char * argv[])
{
    printf("Pick a number to start from "); //this is the numberEntered
    const char *numberEntered = readline(NULL);
    int i = atoi(numberEntered); //atoi converts the string(numberEntered) to an integer. If the string cannot be converted, it returns 0.
    while (i > 0) {
        i = i - 3;
        printf("%d\n" , i);
        if (i %5 == 0) {
            printf("Found one!\n");
        }
    }
    
    return 0;
}

#5

Looks awesome. I’ll have to try implementing more while loops as I go. Thanks for the input.


#6

Here’s how I did it, basically the same, with a couple of mods; I included the ‘Found one’ statement in a single printf with the ints using a conditional inside the for loop, and tested for divisibility by 5 and for ‘not 0’ for this too.
Also, is there a good reason anyone knows not to initialize i until the for loop?
And using “” to print nothing seems clumsy, is there a better way that doesn’t print NULL?

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

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

{
int i;
char *found;

printf("Where should I start counting? ");

const char *startNumber=readline(NULL);

for (i=atoi(startNumber); i>=0; i-=3) {

    if (i % 5==0 && i!=0) {
    found="Found one!";
    } else {found="";}
    
    printf("%d  %s\n",i,found);
    
    }
return 0;

}
[/code]


#7

Whenever I try to build this - even when I directly copy the code that works for others - the build fails and I get two issues in the left hand column

Apple Mach-O Linker (Id) Error
"_readline", referenced from:
_main in main.o
Symbol(s) not found in architecture x86_64

Linker command failed with exit code 1 (use -v to see invocation)

Does anyone know what this means? Thanks.


#8

Did you add the library (or framework), containing the code for the readline function to your project?

Whenever you reference a function that you don’t have the source code for and therefore can’t include its code in your project, you need to add to your project the library or framework containing (the object code for) that function.

[Become a competent programmer faster than you can imagine: pretty-function.org]