mardi 26 août 2014

New CS Student, extended Hello World program


Vote count:

0




I'm taking my first coding class and I'm trying to figure out how to make a Hello, World! program ask for my name and then reply with it. Our instructor gave us the lines


int


main(int argc,char **argv)



{

int x;

printf("hello, world!\n");
printf("give me a number! ");
scanf(" %d",&x);
printf("%d is my favorite number!!\n",x + 1);
return 0;
}


which is a working hello world program when compiled, but we have to make it ask for our name as well, which I cannot figure out.


This was his hint:


//test



#include <string.h>

char s[512]; //allocate an array to hold a string
printf("type in some stuff: ");
fgets(s,sizeof(s),stdin); //read in a string of characters
s[strlen(s) - 1] = '\0'; //remove the newline character
printf ("you typed %s!\n", s);
//NOTE: this code fails if string is larger than 511 characters


but seeing as I know literally nothing about coding it isn't very helpful to me. I have a quiz on this tomorrow (the activity was assigned Monday) so quick help would be appreciated, thanks.


Also, I'm brand new and can't figure out the formatting. Sorry!



asked 1 min ago







New CS Student, extended Hello World program

Aucun commentaire:

Enregistrer un commentaire