hi guys, I am a new student in learning C, I have about a month of experience on it and i am learning now how to use loops.
I wanted to write a program using loops in which i ask them to guess a password to a question i ask. (THis was a story of Ali baba , the person just needs to write open sesame to have the right answer
(!Bear with me if my programming is really bad: i know the highlighted area is wrong, any critics harsh or good will be appreciated thanks)

#include<stdio.h>

void main()
{
    char KEY = 'a';

    char Guess;

    char answer;

    printf("You have traveled the World's Deserts and you have finally found the cave\n"
           "of Ali Baba. You see the cave shut, and you shout the secret words:"      
                );
    scanf("%s",&Guess);
do
{
if (Guess==KEY)  
    printf( "%s You have opened the Cave and took all the riches of the Bandits, Well Done \n",Guess);
  //else if( Guess!=KEY)
  //printf( "You hear the sound of hooves closing in on you its the bandits, Please hurry and try again:");
  //scanf("%s", &Guess);

}while(Guess!=KEY);
printf( "You hear the sound of hooves closing in on you its the bandits, Please hurry and try again:");
  scanf("%s", &Guess);
  system("PAUSE");  
  return 0;
}

>>scanf("%s",&Guess);
The "%s" tells scanf() that Guess is a pointer to a character array, not a single character. If all you want is a single character then call getchar() instead. (link)


If you want them to enter "open sesame", which is two words separated by a space, then its a lot easier to declare Guess as a character array then use fgets()

char Guess[80] = {0};
fgets( Guess, sizeof(Guess), stdin);

@konohastar.

We'll let you off this time ;) since that was your first post, but in future please use code tags when posting code. You'll find members of this forum more willing to help you out if you do this. Read the material at this link to learn how to post code correctly:

http://www.daniweb.com/forums/thread93280.html

>>scanf("%s",&Guess);
The "%s" tells scanf() that Guess is a pointer to a character array, not a single character. If all you want is a single character then call getchar() instead. (link)


If you want them to enter "open sesame", which is two words separated by a space, then its a lot easier to declare Guess as a character array then use fgets()

char Guess[80] = {0};
fgets( Guess, sizeof(Guess), stdin);

To add to AD's suggestions, I would get rid of the system("pause") function call and replace this with getchar() as well. I'm assuming you are doing this because you are running the program from within an IDE and you want to pause execution of the program to give you enough time to observe the output.

thanks for the tips AD.lol and yellow thanks for letting it slide, yep the reason i paused it was for it to pause. Thanks on the tip i will try it out and let you guys know if i made any progress, thanks.:))

To add to AD's suggestions, I would get rid of the system("pause") function call and replace this with getchar() as well. I'm assuming you are doing this because you are running the program from within an IDE and you want to pause execution of the program to give you enough time to observe the output.

THanks guys, lol but still stuck on the program, the one thing that has me lost , could you guys tell me if the loop area could be improved? Currently i tried to redo the program and declared my statement just like you guys told me to but i somehow feel the program crashes due to the loop not being complete

you can also do something like
while(!answer())
reget user choice
//then when its out of the loop
puts("welcome to the treasure of centries ");

if you know about functions ofcourse
and if you dont then you could just just do scanf inside loop and keep scanning till while(strcmp(Name1,Name)) you cant compare char * by just relational operators

THanks guys, lol but still stuck on the program, the one thing that has me lost , could you guys tell me if the loop area could be improved? Currently i tried to redo the program and declared my statement just like you guys told me to but i somehow feel the program crashes due to the loop not being complete

Try the following code snippet out and see if it matches your requirements. I'm assuming that you just wish to detect a single character input and when the correct key is entered, display a "successful" type message and exit the program. Otherwise keep on prompting the user with that "scary" message if he/she does not input the correct key.

#include <stdio.h>

int main() {
    char KEY = 'a';
    int Guess;

    printf("You have traveled the World's Deserts and you have finally found the cave\n"
                "of Ali Baba. You see the cave shut, and you shout the secret words:");

    while ((Guess = getchar()) != KEY) {
        // this line "flushes" the input buffer
        while (getchar() != '\n');
        printf( "You hear the sound of hooves closing in on you its the bandits, Please hurry and try again\n");
    }
    printf("You have opened the Cave and took all the riches of the Bandits, Well Done \n");

    // pause program - enter any key + ENTER to terminate the program
    getchar();
    return 0;
}

A couple of things to note:
1) Using the getchar() function, you need to flush the input buffer to get rid of the newline character, otherwise this character will be read on the next getchar() call and your logic gets all screwed up. I would strongly suggest that you look up the getchar() function in your compiler documentation and study up on it.

2) In your original code posting, you used void main() - this is bad, very bad. At a minimum use int main() as the signature for your main() function.

commented: I like the way you answer. +17

1) Using the getchar() function, you need to flush the input buffer to get rid of the newline character

As a corollary, fflush(stdin) is not the way to flush the input buffer because fflush() is only defined to work on output buffers. A portable way to flush is getchar() in a loop:

while (getchar() != '\n')
{
    /* no work to do */
}

If EOF is possible, add that to the condition:

{
    int c;

    while ((c = getchar()) != '\n' && c != EOF)
    {
        /* no work to do */
    }
}

The only problem with this loop strategy is when the buffer is empty, the loop will block until some characters are buffered. The end user will have to type an extra [Enter] before getting past the loop, and that can be surprising. I like to fix it by dipping into the stream internals, but that is not a portable fix. Use it with caution:

#if defined(_MSC_VER)
/* Visual C and derivatives */
#define FLUSH_STDIN() stdin->_cnt = 0;
#else
#define FLUSH_STDIN() while (getchar() != '\n')
#endif

2) In your original code posting, you used void main() - this is bad, very bad.

I have only seen one OS mentioned that fails when void main() is used, and both compilers in that article warn about it. Not recommended, definitely. void main() is not portable and technically undefined when the compiler does not support it, so it should be avoided on all hosted environments. But 'very bad' is an exaggeration unless you can find a statistically significant number of cases where it causes real problems. I have not found that many cases yet, so I do not see void main() as more than a minor bad habit. But it is common enough that fake experts and pedants can get off on yelling about it. :D

commented: Thanks Tom to share your knowledge. Good suggestion but I still recomend int main() +17
commented: I still prefer to use the standard about the return type of main, but you're right about the void main() :) +23

Thanks guys after looking at all your guidance i was able to rewrite the project and it works great :)) this is what it looks like

#include <stdio.h>

int main()
{
    
//Local Declarations
     int Password = 1;
     char PasswordA; 
     char PasswordB;
//Statements
            //Creation of Loop 
     while (Password)
     {
           //Print out question
           printf("Please enter your 2 letter password: ");
           //input data of the user, C expects 2 characters 
           PasswordA=getchar();
           PasswordB=getchar();
           //Declaring the Condition to making the Loop True or False
     if (PasswordA!='c' || PasswordB!='s');//if password is not c or s than loop back to ask question
     else 

           Password = 0;
           }//end
system("pause");
return 0;
}

Thanks For the help,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.