Question:
C Programming Help?
Jacob
2014-07-19 16:28:51 UTC
I'm using Dev-C++ and writing in just C.

Whenever I run the program it will act normally until I press enter for the location and then skip some questions. And then if it does work it won't print the final paragraph.

I don't know what to do :/

=====code

#include

int main(void)
{
int animal;
int name;
int loc;
int v1;
int num;
int obj1;
int adj;
int obj2;
int mood;
int v2;

printf(“Pick an animal. ==> “);
scanf(“%s”, &animal);

printf(“\nType up a name. ==> “);
scanf(“%s”, &name);

printf(“\nThink of a location. ==> “);
scanf(“%s”, &loc);

printf(“\nGive me a verb. ==> “);
scanf(“%s”, &v1);

printf(“\nChoose a number. ==> “);
scanf(“%d”, &num);

printf(“\nCome up with a random object. ==> “);
scanf(“%s”, &obj1);

printf(“\nWhat’s a good adjective? . ==> “);
scanf(“%s”, &adj);

printf(“\nI need another random object. ==> “);
scanf(“%s”, &obj2);

printf(“\nTell me what mood you’re in. ==> “);
scanf(“%s”, &mood);

printf(“\nFinally, give me another verb. ==> “);
scanf(“%s”, &v2);

printf(“\nOnce upon a time there was a/n %s named %s. They lived in %s and loved to %s with %d friends. But one day a giant %s fell from the sky and blew up %s’s %s %s. %s was so %s that they %sed all the way home.\n\n”, animal, name, loc, v1, num, obj1, name, adj, obj2, name, mood, v2);

getch();
}

=====code
Four answers:
?
2014-07-19 19:17:28 UTC
The problem with scanf is that it requires a format string according to the type of information you want to read in. Most of your items as your last printf statement reveals should have been declared char with some length. This is the problem of your code. I introduced two functions get_number and get_string.They should help you get along with the rest of your code:



With respect to your question: I don't allow for empty input ... :O)

#include

#include

#include



int get_number(const char *cpszPrompt)

{

int iRet = 0;

char buffer[128];

do{

printf("%s ===> ", cpszPrompt);

fgets(buffer, sizeof(buffer), stdin);

}while(sscanf(buffer, "%d", &iRet) != 1);

return iRet;

}



void get_string(const char *cpszPrompt, char *pszString)

{

char buffer[128];

do{

fprintf(stdout, "%s ===> ", cpszPrompt);

fgets(buffer, sizeof(buffer), stdin);

while(isspace(*buffer))

strcpy(buffer, buffer + 1);

int iLen = strlen(buffer) - 1;

while(isspace(buffer[iLen])) {

buffer[iLen--] = 0;

}

if (strlen(buffer) < 1) {

printf("After spaces you have to enter %s, please!\n", cpszPrompt);

}

else {

break;

}

}while(1);

strcpy(pszString, buffer);

}



int main(void)

{

char animal[128];

char name[128];

char loc[128];

char v1[128];

int num;

char obj1[128];

char adj[128];

char obj2[128];

char mood[128];

char v2[128];





get_string("Pick an animal.", animal);

get_string("Type up a name.", name);

get_string("Think of a location.", loc);

get_string("Give me a verb.", v1);

num = get_number("Choose a number.");

get_string("Come up with a random object.", obj1);

get_string("What's a good adjective?", adj);

get_string("I need another random object.", obj2);

get_string("Tell me what mood you’re in.", mood);

get_string("Finally, give me another verb.", v2);

printf("\nOnce upon a time there was a %s named %s."

"\n%s and friends lived in the %s and loved to %s"

"\nwith %d friends. But one day a giant"

"\n%s fell from the sky and blew up %s's"

"\n%s %s. %s was so %s that they %sed"

"\nall the way home.\n\n", animal, name, name, loc, v1, num, obj1, name, adj, obj2, name, mood, v2);

}



Probably I would end up writing the program like this:

#include

#include

#include



int get_number(const char *cpszPrompt)

{

int iRet = 0;

char buffer[128];

do{

printf("%s ===> ", cpszPrompt);

fgets(buffer, sizeof(buffer), stdin);

}while(sscanf(buffer, "%d", &iRet) != 1);

return iRet;

}



void get_string(const char *cpszPrompt, char *pszString)

{

char buffer[128];

do{

fprintf(stdout, "%s ===> ", cpszPrompt);

fgets(buffer, sizeof(buffer), stdin);

while(isspace(*buffer))

strcpy(buffer, buffer + 1);

int iLen = strlen(buffer) - 1;

while(isspace(buffer[iLen])) {

buffer[iLen--] = 0;

}

if (strlen(buffer) < 1) {

printf("After spaces you have to enter %s, please!\n", cpszPrompt);

}

else {

break;

}

}while(1);

strcpy(pszString, buffer);

}



enum {ANIMAL, NAME, LOC, VERB1, OBJ1, ADJ, OBJ2, MOOD, VERB2};

int main(void)

{

char s[9][128];

int num;





get_string("Pick an animal.", s[ANIMAL]);

get_string("Type up a name.", s[NAME]);

get_string("Think of a location.", s[LOC]);

get_string("Give me a verb.", s[VERB1]);

get_string("Come up with a random object.", s[OBJ1]);

num = get_number("Choose a number.");

get_string("What's a good adjective?", s[ADJ]);

get_string("I need another random object.", s[OBJ2]);

get_string("Tell me what mood you’re in.", s[MOOD]);

get_string("Finally, give me another verb.", s[VERB2]);

printf("\nOnce upon a time there was a %s named %s."

"\n%s and friends lived in the %s and loved to %s"

"\nwith %d friends. But one day a giant"

"\n%s fell from the sky and blew up %s's"

"\n%s %s. %s was so %s that they %sed"

"\nall the way home.\n\n", s[ANIMAL], s[NAME], s[NAME], s[LOC], s[VERB1], num, s[OBJ1], s[NAME], s[ADJ], s[OBJ2], s[NAME], s[MOOD], s[VERB2]);

}

Have fun!
?
2014-07-20 01:34:38 UTC
you have several problems:

#include



int main(void)

{

char animal[100];

char name[100];

char loc[100];

char v1[100];

int num; // this one was right..

char obj1[100];

char ad[100]j;

int obj2[100];

int mood[100];

int v2;



printf(“Pick an animal. ==> “);

scanf(“%s”, animal); // for strings you do not need the & the array name by itself is a pointer to the string



printf(“\nType up a name. ==> “);

scanf(“%s”, name);

...

printf(“\nChoose a number. ==> “);

scanf(“ %d”, &num); // you do need the & here to get the address of num since it is not an array...(and you need the space before the % so scanf skips any whitespace from before the number (such as leftover '\n's
Andy T
2014-07-20 01:19:33 UTC
int? Should it not char* (String).



There's your problem, under Windows you won't get total crash the way Linux would've done to alert you. You essentially wrecked that little memory space your program ran inside, like I said, Windows will not segfault your program immediately to alert you.
?
2014-07-19 23:44:16 UTC
well you need to fix your data type . name , loc , animal etc.

Those variable where you want to enter an string (not a number) must be of string data type or of character array type

like



char [100] name ;

char [100] animal; etc



where ever you are using "%s" , that variable must be of string or character array data type


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...