crzyone9584 Posted October 2, 2011 Author Share Posted October 2, 2011 I was wondering if there was a better way of doing this? The teacher asked for brackets so I used brackets but I'm not sure if there is a easier way to use this.> /*********************************************************************************/> /* Topic 4 - Problem #3 Chapter #2 Page 48 - Example Program */> /* Programmer: Jeff Padfield */> /* September, 2011 */> /* User inputs their name */ > /* Program out puts Welcome, "insert name here" */ > /*********************************************************************************/> /* Include a header file for printing text */> /*********************************************************************************/> #include #include #include /*********************************************************************************/> /* Declare Variables */> /*********************************************************************************/> char userName[80] = { 0 };> //userName = '\0';> /*********************************************************************************/> /* Main Function */> /*********************************************************************************/> int main()> {> printf("Please enter your name: \n");> scanf("%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]", userName);> printf("Welcome %s \n", userName);> return(0);> system("pause");> > }It works just fine but for the scanf() is there a better way to have the brackets with abc… in it? Link to comment Share on other sites More sharing options...
dthnote801 Posted October 2, 2011 Share Posted October 2, 2011 Why not have a variable to hold "abc…" in it? Or better yet just have the user input the name?Like...```int main(){ printf("Please enter your name: \n"); scanf(userName); printf("Welcome %c \n", userName); return(0); system("pause");}```(Obviously seen that the teacher asked for brackets, but this just seems like a much better way for using scanf.) Link to comment Share on other sites More sharing options...
crzyone9584 Posted October 2, 2011 Author Share Posted October 2, 2011 Dont you have to have a conversion specifier in the scanf() like %d, %f, %c, or %s? Or is my book lieing to me? Link to comment Share on other sites More sharing options...
dthnote801 Posted October 2, 2011 Share Posted October 2, 2011 @crzy:> Dont you have to have a conversion specifier in the scanf() like %d, %f, %c, or %s? Or is my book lieing to me?I don't put it there and it seems to work just fine, but then again most of the time I just use fgets. Link to comment Share on other sites More sharing options...
crzyone9584 Posted October 2, 2011 Author Share Posted October 2, 2011 Don't think i got that far in the book yet. The hw was to use scanf() with [] so thats what i did. Link to comment Share on other sites More sharing options...
dthnote801 Posted October 3, 2011 Share Posted October 3, 2011 Yeah, I never heard of using brackets in scanf() so I really can't say to be honest. None of the sites I'm using has never said anything about it. Link to comment Share on other sites More sharing options...
crzyone9584 Posted October 7, 2011 Author Share Posted October 7, 2011 Looking for more C help guys. I have done the minimum for the assignment now I'm trying to get the goto command to work. Any suggestion on what I'm doing wrong?```#include #include int iRandom = 0;int input = 0;char CRepsonseYorN = '\0';;main(){ // Generate random numbers iRandom = (rand() % 10) +1; // Welcome player and ask if he/she would like to play printf("Welcome to the number guessing game!\n"); continue: printf("Would you like to play? Y or N...\n"); scanf("%c", &CRepsonseYorN); if ( CRepsonseYorN == 'Y' || CRepsonseYorN == 'y' ) { // user has decided to play printf("Input a number from 1 - 10 please!\n"); scanf("%d", &input); if ( input == iRandom) { printf("Congrats you have picked the right number\n"); goto continue; } else { // Input wrong number or input a character printf("You did not input the correct number you input was a character\n"); printf("Please try again\n"); goto continue; } } else { if ( CRepsonseYorN == 'N' || CRepsonseYorN == 'n' ) { // user has decided not to play printf("Thank you for your input. Come play again!\n"); system("pause"); } }}``` Link to comment Share on other sites More sharing options...
LeaRae Posted October 25, 2011 Share Posted October 25, 2011 New code, with notes. Hope the assignment isn't late :)#firstpost```#include #include int iRandom = 0;int input = 0;char CRepsonseYorN = '\0';// we need a number to flag our while loopint bGameRunningFlag = 1; // treat 1 as true, so 0 means false?#DEFINE TRUE 1#DEFINE FALSE 0main(){ // you should seed your randomizer before you call it. // the function is called srand() and you usually pass a time to it. // Generate random numbers iRandom = (rand() % 10) +1; // Welcome player and ask if he/she would like to play printf("Welcome to the number guessing game!\n"); // using GOTO is considered bad practice. // It makes code difficult to maintain, // hard to read, and it's almost never necessary // as a rule of thumb, "never use GOTO when something else can do the job" // so, we can remove your //continue: // and use a loop instead bGameRunningFlag = TRUE; while(bGameRunningFlag == TRUE) { printf("Would you like to play? Y or N...\n"); scanf("%c", &CRepsonseYorN); if ( CRepsonseYorN == 'Y' || CRepsonseYorN == 'y' ) { // user has decided to play printf("Input a number from 1 - 10 please!\n"); scanf("%d", &input); if ( input == iRandom) { printf("Congrats you have picked the right number\n"); // don't need this; when the loop is done it will restart the game //goto continue; } else { // Input wrong number or input a character printf("You did not input the correct number you input was a character\n"); printf("Please try again\n"); // don't need this; when the loop is done it will restart the game //goto continue; } } else { if ( CRepsonseYorN == 'N' || CRepsonseYorN == 'n' ) { // user has decided not to play printf("Thank you for your input. Come play again!\n"); // this is dirty, move system("pause"); to the end of the program // and let the loop fall through when you set the flag to false //system("pause"); bGameRunningFlag = FALSE; // next time around the loop, it will evaluate FALSE and not run // so the program will exit, woohoo! } } } system("pause");}``` Link to comment Share on other sites More sharing options...
Admiral Refuge Posted October 25, 2011 Share Posted October 25, 2011 What's with all these global variables outside main()? Link to comment Share on other sites More sharing options...
LeaRae Posted October 26, 2011 Share Posted October 26, 2011 in a one function program it's not necessary, but as soon as you make anything larger in C they're necessary.. not really a better option in that language Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now