Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,837 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




"unique key is pressed, indicating that the user wishes to exit t

 
Reply to this topicStart new topic

"unique key is pressed, indicating that the user wishes to exit t, help..

yuentong
3 Oct, 2008 - 03:54 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 17

CODE

/*
Question:

Write a C program that prompt the user for his or her date of birth,
before determining his or her Western Zodiac Sign.
The program must remain in an infinite loop
(prompt the user for his or her date of birth again, after each display of the previous result)
until a unique key is pressed, indicating that the user wishes to exit the program.
*/


#include<stdio.h>
void main()
{
    int month;
    int day;
    int WZS(int day,int month);
                int e;

    printf("Please enter your month of birth:\n");
    scanf("%d",&month);
    printf("Please enter your day of birth:\n");
    scanf("%d",&day);

    do
                {
        if((day>=22)&&(day<=31)&&(month==12))
            printf("Your western zodiac sign are Capricorn\n");
        else if((day>=1)&&(day<=20)&&(month==1))
            printf("Your western zodiac sign are Capricorn\n");
        else if((day>=21)&&(day<=31)&&(month==1))
            printf("Your western zodiac sign are Aquarius!\n");
        else if((day>=1)&&(day<=21)&&(month==2))
            printf("Your western zodiac sign are Aquarius\n");
        else if((day>=22)&&(day<29)&&(month==2))
            printf("Your western zodiac sign are Pisces\n");
        else if((day>=1)&&(day<=21)&&(month==3))
            printf("Your western zodiac sign are Pisces\n");
        else if((day>=22)&&(day<=31)&&(month==3))
            printf("Your western zodiac sign are Aries\n");
        else if((day>=1)&&(day<=20)&&(month==4))
            printf("Your western zodiac sign are Aries\n");
        else if((day>=21)&&(day<=30)&&(month==4))
            printf("Your western zodiac sign are Taurus\n");
        else if((day>=1)&&(day<=21)&&(month==5))
            printf("Your western zodiac sign are Taurus\n");
        else if((day>=22)&&(day<=31)&&(month==5))
            printf("Your western zodiac sign are Gemini\n");
        else if((day>=1)&&(day<=21)&&(month==6))
            printf("Your western zodiac sign are Gemini\n");
        else if((day>=22)&&(day<=30)&&(month==6))
            printf("Your western zodiac sign are Cancer\n");
        else if((day>=1)&&(day<=22)&&(month==7))
            printf("Your western zodiac sign are Cancer\n");
        else if((day>=23)&&(day<=31)&&(month==7))
            printf("Your western zodiac sign are Leo\n");
        else if((day>=1)&&(day<=22)&&(month==8))
            printf("Your western zodiac sign are Leo\n");
        else if((day>=23)&&(day<=31)&&(month==8))
            printf("Your western zodiac sign are Virgo\n");
        else if((day>=1)&&(day<=22)&&(month==9))
            printf("Your western zodiac sign are Virgo\n");
        else if((day>=23)&&(day<=30)&&(month==9))
            printf("Your western zodiac sign are Libra\n");
        else if((day>=1)&&(day<=23)&&(month==10))
            printf("Your western zodiac sign are Libra\n");
        else if((day>=24)&&(day<=31)&&(month==10))
            printf("Your western zodiac sign are Scorpio\n");
        else if((day>=1)&&(day<=21)&&(month==11))
            printf("Your western zodiac sign are Scorpio\n");
        else if((day>=22)&&(day<=30)&&(month==11))
            printf("Your western zodiac sign are Sagittarius\n");
        else if((day>=1)&&(day<=21)&&(month==12))
            printf("Your western zodiac sign are Sagittarius\n");
        else
            printf("Wrong data!\n");
    } while(WZS!=e);

    
}


User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
3 Oct, 2008 - 05:13 PM
Post #2

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,111



Thanked: 8 times
Expert In: Hardware, Networking

My Contributions
What's your question?
User is online!Profile CardPM
+Quote Post

Sadaiy
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 05:41 AM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 38



Thanked: 1 times
My Contributions
ok here it is, first of all it should be int main() not void main(), secondly if you want the input to be part of the loop you have to put that inside the loop as i did, and lastly to stop the loop i used getchar() == 'e' inside the while() of the loop. That is all i changed.

CODE

#include <stdio.h>



int main()
{
    int month;
    int day;
    
  

    do
    {
         printf("Please enter your month of birth:\n");
        scanf("%d",&month);
        printf("Please enter your day of birth:\n");
        scanf("%d",&day);
        
        
        
        if((day>=22)&&(day<=31)&&(month==12))
            printf("Your western zodiac sign are Capricorn\n");
        else if((day>=1)&&(day<=20)&&(month==1))
            printf("Your western zodiac sign are Capricorn\n");
        else if((day>=21)&&(day<=31)&&(month==1))
            printf("Your western zodiac sign are Aquarius!\n");
        else if((day>=1)&&(day<=21)&&(month==2))
            printf("Your western zodiac sign are Aquarius\n");
        else if((day>=22)&&(day<29)&&(month==2))
            printf("Your western zodiac sign are Pisces\n");
        else if((day>=1)&&(day<=21)&&(month==3))
            printf("Your western zodiac sign are Pisces\n");
        else if((day>=22)&&(day<=31)&&(month==3))
            printf("Your western zodiac sign are Aries\n");
        else if((day>=1)&&(day<=20)&&(month==4))
            printf("Your western zodiac sign are Aries\n");
        else if((day>=21)&&(day<=30)&&(month==4))
            printf("Your western zodiac sign are Taurus\n");
        else if((day>=1)&&(day<=21)&&(month==5))
            printf("Your western zodiac sign are Taurus\n");
        else if((day>=22)&&(day<=31)&&(month==5))
            printf("Your western zodiac sign are Gemini\n");
        else if((day>=1)&&(day<=21)&&(month==6))
            printf("Your western zodiac sign are Gemini\n");
        else if((day>=22)&&(day<=30)&&(month==6))
            printf("Your western zodiac sign are Cancer\n");
        else if((day>=1)&&(day<=22)&&(month==7))
            printf("Your western zodiac sign are Cancer\n");
        else if((day>=23)&&(day<=31)&&(month==7))
            printf("Your western zodiac sign are Leo\n");
        else if((day>=1)&&(day<=22)&&(month==8))
            printf("Your western zodiac sign are Leo\n");
        else if((day>=23)&&(day<=31)&&(month==8))
            printf("Your western zodiac sign are Virgo\n");
        else if((day>=1)&&(day<=22)&&(month==9))
            printf("Your western zodiac sign are Virgo\n");
        else if((day>=23)&&(day<=30)&&(month==9))
            printf("Your western zodiac sign are Libra\n");
        else if((day>=1)&&(day<=23)&&(month==10))
            printf("Your western zodiac sign are Libra\n");
        else if((day>=24)&&(day<=31)&&(month==10))
            printf("Your western zodiac sign are Scorpio\n");
        else if((day>=1)&&(day<=21)&&(month==11))
            printf("Your western zodiac sign are Scorpio\n");
        else if((day>=22)&&(day<=30)&&(month==11))
            printf("Your western zodiac sign are Sagittarius\n");
        else if((day>=1)&&(day<=21)&&(month==12))
            printf("Your western zodiac sign are Sagittarius\n");
        else
            printf("Wrong data!\n");

        
    }while(getchar() != 'e');

    return 0;
}

User is offlineProfile CardPM
+Quote Post

yuentong
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 12:42 PM
Post #4

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 17

QUOTE(Sadaiy @ 4 Oct, 2008 - 06:41 AM) *

ok here it is, first of all it should be int main() not void main(), secondly if you want the input to be part of the loop you have to put that inside the loop as i did, and lastly to stop the loop i used getchar() == 'e' inside the while() of the loop. That is all i changed.

CODE

#include <stdio.h>



int main()
{
    int month;
    int day;
    
  

    do
    {
         printf("Please enter your month of birth:\n");
        scanf("%d",&month);
        printf("Please enter your day of birth:\n");
        scanf("%d",&day);
        
        
        
        if((day>=22)&&(day<=31)&&(month==12))
            printf("Your western zodiac sign are Capricorn\n");
        else if((day>=1)&&(day<=20)&&(month==1))
            printf("Your western zodiac sign are Capricorn\n");
        else if((day>=21)&&(day<=31)&&(month==1))
            printf("Your western zodiac sign are Aquarius!\n");
        else if((day>=1)&&(day<=21)&&(month==2))
            printf("Your western zodiac sign are Aquarius\n");
        else if((day>=22)&&(day<29)&&(month==2))
            printf("Your western zodiac sign are Pisces\n");
        else if((day>=1)&&(day<=21)&&(month==3))
            printf("Your western zodiac sign are Pisces\n");
        else if((day>=22)&&(day<=31)&&(month==3))
            printf("Your western zodiac sign are Aries\n");
        else if((day>=1)&&(day<=20)&&(month==4))
            printf("Your western zodiac sign are Aries\n");
        else if((day>=21)&&(day<=30)&&(month==4))
            printf("Your western zodiac sign are Taurus\n");
        else if((day>=1)&&(day<=21)&&(month==5))
            printf("Your western zodiac sign are Taurus\n");
        else if((day>=22)&&(day<=31)&&(month==5))
            printf("Your western zodiac sign are Gemini\n");
        else if((day>=1)&&(day<=21)&&(month==6))
            printf("Your western zodiac sign are Gemini\n");
        else if((day>=22)&&(day<=30)&&(month==6))
            printf("Your western zodiac sign are Cancer\n");
        else if((day>=1)&&(day<=22)&&(month==7))
            printf("Your western zodiac sign are Cancer\n");
        else if((day>=23)&&(day<=31)&&(month==7))
            printf("Your western zodiac sign are Leo\n");
        else if((day>=1)&&(day<=22)&&(month==8))
            printf("Your western zodiac sign are Leo\n");
        else if((day>=23)&&(day<=31)&&(month==8))
            printf("Your western zodiac sign are Virgo\n");
        else if((day>=1)&&(day<=22)&&(month==9))
            printf("Your western zodiac sign are Virgo\n");
        else if((day>=23)&&(day<=30)&&(month==9))
            printf("Your western zodiac sign are Libra\n");
        else if((day>=1)&&(day<=23)&&(month==10))
            printf("Your western zodiac sign are Libra\n");
        else if((day>=24)&&(day<=31)&&(month==10))
            printf("Your western zodiac sign are Scorpio\n");
        else if((day>=1)&&(day<=21)&&(month==11))
            printf("Your western zodiac sign are Scorpio\n");
        else if((day>=22)&&(day<=30)&&(month==11))
            printf("Your western zodiac sign are Sagittarius\n");
        else if((day>=1)&&(day<=21)&&(month==12))
            printf("Your western zodiac sign are Sagittarius\n");
        else
            printf("Wrong data!\n");

        
    }while(getchar() != 'e');

    return 0;
}



thanks ya...sadaiy

User is offlineProfile CardPM
+Quote Post

yuentong
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 12:57 PM
Post #5

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 17

after compiling when i press "e" ..
it will come out somethings like this..

[please enter your month of birth:
e (this is where i press e)
please enter your day of birth: (can i make here straight away to be please enter another key to cotinue?)
your western zodiac is leo. (here are very weird.after pressing e..it should not coming out right?)

please enter another key to continue]

anyone who can help? thanks ya..

This post has been edited by yuentong: 4 Oct, 2008 - 12:58 PM
User is offlineProfile CardPM
+Quote Post

Sadaiy
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 03:13 PM
Post #6

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 38



Thanked: 1 times
My Contributions
it is going in order of operations. it will continue through the loop until it gets to the bottom where it checks the while().. try making it a while loop instead of a do{}while();... that might work, i'm not sure though

cpp

while(getchar() != 'e')
{
printf("Please enter your month of birth:\n");
scanf("%d",&month);
printf("Please enter your day of birth:\n");
scanf("%d",&day);



if((day>=22)&&(day<=31)&&(month==12))
printf("Your western zodiac sign are Capricorn\n");
else if((day>=1)&&(day<=20)&&(month==1))
printf("Your western zodiac sign are Capricorn\n");
else if((day>=21)&&(day<=31)&&(month==1))
printf("Your western zodiac sign are Aquarius!\n");
else if((day>=1)&&(day<=21)&&(month==2))
printf("Your western zodiac sign are Aquarius\n");
else if((day>=22)&&(day<29)&&(month==2))
printf("Your western zodiac sign are Pisces\n");
else if((day>=1)&&(day<=21)&&(month==3))
printf("Your western zodiac sign are Pisces\n");
else if((day>=22)&&(day<=31)&&(month==3))
printf("Your western zodiac sign are Aries\n");
else if((day>=1)&&(day<=20)&&(month==4))
printf("Your western zodiac sign are Aries\n");
else if((day>=21)&&(day<=30)&&(month==4))
printf("Your western zodiac sign are Taurus\n");
else if((day>=1)&&(day<=21)&&(month==5))
printf("Your western zodiac sign are Taurus\n");
else if((day>=22)&&(day<=31)&&(month==5))
printf("Your western zodiac sign are Gemini\n");
else if((day>=1)&&(day<=21)&&(month==6))
printf("Your western zodiac sign are Gemini\n");
else if((day>=22)&&(day<=30)&&(month==6))
printf("Your western zodiac sign are Cancer\n");
else if((day>=1)&&(day<=22)&&(month==7))
printf("Your western zodiac sign are Cancer\n");
else if((day>=23)&&(day<=31)&&(month==7))
printf("Your western zodiac sign are Leo\n");
else if((day>=1)&&(day<=22)&&(month==8))
printf("Your western zodiac sign are Leo\n");
else if((day>=23)&&(day<=31)&&(month==8))
printf("Your western zodiac sign are Virgo\n");
else if((day>=1)&&(day<=22)&&(month==9))
printf("Your western zodiac sign are Virgo\n");
else if((day>=23)&&(day<=30)&&(month==9))
printf("Your western zodiac sign are Libra\n");
else if((day>=1)&&(day<=23)&&(month==10))
printf("Your western zodiac sign are Libra\n");
else if((day>=24)&&(day<=31)&&(month==10))
printf("Your western zodiac sign are Scorpio\n");
else if((day>=1)&&(day<=21)&&(month==11))
printf("Your western zodiac sign are Scorpio\n");
else if((day>=22)&&(day<=30)&&(month==11))
printf("Your western zodiac sign are Sagittarius\n");
else if((day>=1)&&(day<=21)&&(month==12))
printf("Your western zodiac sign are Sagittarius\n");
else
printf("Wrong data!\n");


}



User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 03:17 PM
Post #7

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 597



Thanked: 60 times
Dream Kudos: 50
My Contributions
Here's a little something I just whipped up, and perhaps may help you out.
cpp
#include <stdio.h>

static const char *months[] = { "January",
"Feburary",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };

static const int days_per_month[] = { 31,
29,
30,
31,
30,
31,
30,
31,
30,
31,
30,
31 };

int main()
{
unsigned int month = 0, day = 0;
for(;;)
{
printf("Please enter your month of birth: ");
if (1 != scanf("%d", &month))
{
// User did not enter a valid number, so exit
break;
}

printf("Please enter your day of birth: ");
if (1 != scanf("%d", &day))
{
// User did not enter a valid number, so exit
break;
}

if (month > 12 || month < 1)
{
printf("Invalid month\n");
continue;
}
if (day < 1 || day > 31)
{
printf("Invalid day\n");
continue;
}
if (day > days_per_month[month - 1])
{
printf("Dumbass, %s only has, at the most, %d days!\nTry again!\n",
months[month - 1], days_per_month[month - 1]);
continue;
}

printf("Month: %d Day %d\n", month, day);
}

return 0;
}

User is offlineProfile CardPM
+Quote Post

baavgai
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 05:27 PM
Post #8

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,040



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
You never asked the user to enter anything. More importantly, the getchar() is always going to read rest of the buffer input. You'll never actually get a chance to enter the key you want to read.

Put your logic in a function and call that. Here's one way to do your call loop:

cpp

#include <stdio.h>
#include <stdlib.h>

void showSign(int month, int day) {
// your code here
}

int main() {
int month, day;
char input;

do {
printf("Please enter your month of birth:\n");
scanf("%d",&month);
printf("Please enter your day of birth:\n");
scanf("%d",&day);
showSign(month, day);

// Note C doesn't read just a single character
// you'll need to hit enter before it starts to look an things
printf("Press any key to continue (e to exit)\n");
// note the space in front of the %c, it's important.
scanf(" %c",&input);
} while(input != 'e');

return 0;
}


User is offlineProfile CardPM
+Quote Post

UG Cyber
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 05:37 PM
Post #9

D.I.C Head
**

Joined: 24 Jul, 2008
Posts: 185



Thanked: 3 times
My Contributions
Does GetAsyncKeyState() work in C?
if so just multi-thread and set the loop like this
CODE

int Exit;
DWORD WINAPI WaitForKey(LPVOID)
{
    while (true)
   {
         if (GetAsyncKeyState(VK_F2) != NULL)
         {
                 Exit = 1;
         }
    }
}

int main(void)
{
    while (Exit = 0)
    {
     //blah
     //blah
     //blah
     }
      cout << "Good Bye ";
      cout << (char) 1;   //Because i like the smilie
      Sleep(2000);
      return 0;
}


User is offlineProfile CardPM
+Quote Post

baavgai
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 06:01 PM
Post #10

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,040



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(UG Cyber @ 4 Oct, 2008 - 09:37 PM) *

Does GetAsyncKeyState() work in C?


Yes, but only in Windows and not a console program. tongue.gif
User is offlineProfile CardPM
+Quote Post

UG Cyber
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
4 Oct, 2008 - 06:29 PM
Post #11

D.I.C Head
**

Joined: 24 Jul, 2008
Posts: 185



Thanked: 3 times
My Contributions
QUOTE(baavgai @ 4 Oct, 2008 - 07:01 PM) *

QUOTE(UG Cyber @ 4 Oct, 2008 - 09:37 PM) *

Does GetAsyncKeyState() work in C?


Yes, but only in Windows and not a console program. tongue.gif


icon_down.gif
User is offlineProfile CardPM
+Quote Post

yuentong
RE: "unique Key Is Pressed, Indicating That The User Wishes To Exit T
5 Oct, 2008 - 12:34 AM
Post #12

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 17

thanks ya!!! ;p
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 05:09PM

Live C++ Help!

C++ Tutorials