I’m assuming this was added to the 2nd edition because I can’t find anything on it yet. Anyways for my solution I got
printf(“Where should I start counting?”);
const char *num = readline(NULL);
for (int i = atoi(num); i >= 0; i -= 3) {
printf("%d\n", i);
if (i % 5 == 0) {
printf(“Found one!\n”);
}
}
return 0;
I got the correct output, but I’m still not positive that’s exactly right. I had to import <stdlib.h> to get “atoi” to work, which was not anywhere in the text about atoi. Before I did that I kept getting an error message saying “Implicit declaration of function ‘atoi’ is invalid in C99”, so I went to google for help. Can somebody let me know if I’m right, or at least on the right track? Brand-new coder here so patience is appreciated. Thanks!
However, here are a few things to consider. The readline () function can return a string that doesn’t start with a number. Then, what does atoi () give you if you feed it with such a string?
Rather than using the function atoi, better to use the sscanf function:
int i;
if (sscanf (num, "%d", &i) != 1) {
// Handle error
...
} else {
for (; i >= 0; i -= 3) {
...
}
}
sscanf function returns the number of successful conversions from the input string.
// Uses import instead of include to match example in book, but either work
#import <readline/readline.h> // Needed for getting text entered by user
#import <stdio.h>
// #include <stdlib.h> - If this was used then the atoi() function would not need to be declared before use
int atoi(); // Function needs to be declared before use or a warning for implicit declaration is received
int main(int argc, const char * argv[])
{
// Challenge: user input - OPTION 2 (using atoi in while loop)
// Ask user for input
printf("Where should I start counting? ");
// Gets numerical string user typed in and stores it in variable startCount
const char *startCount = readline(NULL);
// Convert numerical string into an integer by calling atoi function
int i = atoi(startCount);
// Count backwards from integer through 0 by 3, printing each number
while (i >= 0) {
printf("%d\n", i);
// If number is divisible by 5, print words "Found one!"
if (i % 5 == 0) {
printf("Found one!\n");
}
i-=3; // Decrement by 3
}
/* Challenge: user input - OPTION 1 (using atoi function in for loop)
printf("Where should I start counting? ");
const char *startCount = readline(NULL);
int i;
for (i = atoi(startCount); i >= 0; i-=3) {
printf("%d\n", i);
// If number is divisible by 5, print words "Found one!"
if (i % 5 == 0) {
printf("Found one!\n");
}
}
*/
/* Challenge: counting down
int i;
// Count backwards from 99 through 0 by 3, printing each number
for (i = 99; i >= 0; i-=3) {
printf("%d\n", i);
// If number is divisible by 5, print words "Found one!"
if (i % 5 == 0) {
printf("Found one!\n");
}
}
*/
return 0;
}
[quote=“jamoli85”]I’m assuming this was added to the 2nd edition because I can’t find anything on it yet. Anyways for my solution I got
printf(“Where should I start counting?”);
const char *num = readline(NULL);
for (int i = atoi(num); i >= 0; i -= 3) {
printf("%d\n", i);
if (i % 5 == 0) {
printf(“Found one!\n”);
}
}
return 0;
I got the correct output, but I’m still not positive that’s exactly right. I had to import <stdlib.h> to get “atoi” to work, which was not anywhere in the text about atoi. Before I did that I kept getting an error message saying “Implicit declaration of function ‘atoi’ is invalid in C99”, so I went to google for help. Can somebody let me know if I’m right, or at least on the right track? Brand-new coder here so patience is appreciated. Thanks![/quote]
I am also new, got the book Friday.
If I put your program in my Xcode, it does not function. Why not, if it works in your xcode?
this is my option
printf(“Vanaf waar gaan we beginnen?”);
const char *name = readline(NULL);
int i;
for (i=atoi(name); i>= 0; i=i-3){ //eerste de loop van 3
if(i%3 ==0){ //moet deelbar zijn door 3
printf("%d.\n", i); // op het scherm als if hierboven waar is
if(i%5 ==0){ //moet deelbaar zijn door 5
printf(“Yipee, I have found one…!\n”); // een zin op het scherm als de vorige if waar is
without the first two lines and atoi(name) changed into 99 it worked perfectly.
I understand I can copy any solution, but I would like to solve this by the book and that I know what I am doing, so to say
Hi, I am newbie to Objective-C programming and having issue with atoi() function.
Apparently, when I input number (integer) at output section, the error message shows up like “^CProgram ended with exit code: -1” and also shows “Ch8challengePt2 exited unexpectedly --Lost connection” tab on the top of Xcode screen.
When I input ASCII character, like “name”, there is no error code shows up but no proper solution.
#include <readline/readline.h>
#include <stdio.h>
int main(int argc, const char * argv[])
{
printf("Where should I start counting? ");
const char *number = readline(NULL);
int atoi ();
for (int i = atoi(number); i >= 0; i -= 3){//the last step states i-th number minus 3 to decrease a value (equal sign)
printf("%d.\n",i);
if (i % 5 == 0){ //this is the statement for i-th number devisible by 5 that can be 0 or no decimal point left. == is check for equal
printf("Found one! The number is divisible by 5.\n");
}
}
return 0;
}
Here’s my solution, the only different between this and the non-user-input version is where ‘i’ is assigned a value, and the inclusion of the first printf() call.
#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
printf("Where should I start counting? ");
int i = atoi(readline(NULL));
while (i--) {
if (i % 3 != 0) {
continue;
}
printf("%d\n", i);
if (i % 5 == 0) {
printf("Found one!\n");
}
}
return 0;
}
Here is what I have. The book says nothing about adding #include <stdlib.h>.
Thanks ibex10 for the help!!
Also, can someone look at my code to see if everything looks good. Newbie here
#import <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
int i;
printf("Please enter your starting number: ");
const char *startNumber = readline(NULL);
for (i = atoi(startNumber); i >= 0; i-=3) {
printf("%d\n", i);
if (i % 5 == 0) {
printf("Found One!\n");
}
}
}
// Prompt user for input
printf("Where should I begin counting?");
// Store input in "input"
const char *input = readline(NULL);
printf("--> %s\n", input);
for (int i = atoi(input); i >= 0; i -= 3) {
printf("%d", i);
if (i % 5 == 0) {
printf("Found one!");
}
}
return 0;
// Prompt user for input
printf("Where should I begin counting?");
// Store input in "input"
const char *input = readline(NULL);
printf("--> %s\n", input);
for (int i = atoi(input); i >= 0; i -= 3) {
printf("%d", i);
if (i % 5 == 0) {
printf("Found one!");
}
}
return 0;
}[/code]
Anyone have a suggestion?
Thanks.[/quote]
I was having this problem for HOURS! The program would exit in unexpectantly/lose connection whenever I used the “enter” key on the numeric keypad… However, if you used the other “enter” key found on the keyboard the program exits without any issues. WOW, I thought I was going crazy, because there were times where my code would work and then there were times where it would fail. Little did I know it was all related to which enter key I ended up using.
Without checking the above (so I apologize if this is mentioned but I wanted to find it myself), I got the solution by below. However, with the libreadline library added, and using the (atoi) function used it seems the (atoi) still give a comment but the program ran.[code] #include <stdio.h> #import <readline/readline.h>
int main(int argc, const char * argv[])
{
printf("Where should I start counting? ");
const char *start = readline(NULL);
printf(“Start counting at %s!\n\n”, start);
int i;
for (i = atoi(start); i > 0; i = i - 3) {
if (i % 5 == 0) {
// % returns the left over of a division
printf("Found one!\n");
continue;
}
printf("%d\n", i);
}
printf("Done!\n");
return 0;
}[/code]
I’m honestly a little annoyed as there are so many ‘little’ errors, and things they just point to while not explaining how to get there. In this case, they explained:
[quote]You can use atoi() to convert that string into an integer with a value of 23, which you can happily store in a variable of that type int:
That’s probably fair enough but for this challenge, the string was unknown as it would be an input, and no word on how to do that. So I hope my solution above is a valid one, at least it worked as it should). Within the the Chapter 8, the Loops diagrams are not correct. Specifically for the ‘Continue’ Loop, the diagram is way off from what is really happening. According to this, the break skips to the final printf which is incorrect! The break merely skips one loop.
I keep on going with the book, it’s still a great help to get into the material and it does make me think, so that’s an absolute plus…
int main(int argc, const char * argv[])
{
int i = atoi(readline(“Where shall we begin?\n”));
while (i >=0) {
printf("%d\n", i);
if (i % 5 == 0) {
printf(“Found one!\n”);
}
i -= 3;
}
return 0;
}[/code]
I had the same atoi() issue everyone else mentioned. Oddly enough I was able to make it work without including stdlib.h by breaking it out into it’s own line like this:
int main(int argc, const char * argv[])
{
printf(“Where shall we begin?\n”);
const char *j = readline(NULL);
int i = atoi(j);
while (i >=0) {
printf("%d\n", i);
if (i % 5 == 0) {
printf(“Found one!\n”);
}
i -= 3;
}
return 0;
}[/code]
…which actually confuses me even more? how was it able to use atoi() if the library wasn’t included?!?
Edited: …to include #include statements in the second code snippet.
[quote]…which actually confuses me even more? how was it able to use atoi() if the library wasn’t included?!?
[/quote]
Without #include <stdlib.h>, you mean you did not even get a compilation warning (implicit declaration of function…)?
[quote=“ibex10”][quote]…which actually confuses me even more? how was it able to use atoi() if the library wasn’t included?!?
[/quote]
Without #include <stdlib.h>, you mean you did not even get a compilation warning (implicit declaration of function…)?[/quote]
I did get a warning, but the “Build Succeeded” and I got the correct output.
(I just realized I didn’t include the #include statements in my second code snippet - updating it now)
Thanks and well done to all for the “#include <stdlib.h>” tip. \n
That’s all it took to make it work. “Challenge accepted” but not cleared without you guys.