Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Some C help.


crzyone9584
 Share

Recommended Posts

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

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

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

  • 3 weeks later...
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 loop
int bGameRunningFlag = 1; // treat 1 as true, so 0 means false?
#DEFINE TRUE 1
#DEFINE FALSE 0

main()
{

// 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...