-
February 25th, 2005, 10:11 PM
#1
C Loop question
Hi, I am currenlty updating a calculator that I made a while ago. Here is part of the code
printf("\nWould you like to see the instructions (y) or (n): ");
scanf(" %c", &answer); // gets users answer of yes or no
while ((toupper(answer) != 'Y') || (toupper(answer) != 'N'))
{ printf("\nPLease enter Y or N: ");
scanf(" %c", &answer); // Makes sure user enters a y or n
}
if (toupper(answer) == 'Y')
{prinntf("Instructions")
}
if (toupper(answer) =='N')
{ printf("Fine! \n");
}
/*********************/
then the program continues here...
Is there a way for me too loop using 'while' to make sure the user inputs a number when asked for it?for example:
printf("\nEnter calculation: ");
scanf(" %f", &amount1); //gets the first digits
scanf(" %c", &symbol); //gets calc symbol /*Must be in this order form*/
scanf(" %f", &amount2); //gets the last digits
if (symbol == '/')
{ total = amount1 / amount2; //divides 1st and 2nd amount
printf("The total is %f. \n", total);
}
can i use something for example:
while (amount1 != ANUMBER)
{do this} // so the program can make sure the user inputs a number, and nothing else but a number...
Becuase if they input anything else it comes upt with bad results... Can anyone help me ? thanks
-
February 26th, 2005, 02:56 AM
#2
And i forgot to add... How can i add the current time, like Month/Day/Year, and current time, in the code with C.??? Does anyone know how?
-
February 26th, 2005, 04:49 AM
#3
Junior Member
kyrios,
I think time.h header file should contain important info for u.
u can refer in ur man or look here:
http://www.opengroup.org/onlinepubs/...sh/time.h.html
do tell if it dosent help
-
February 26th, 2005, 06:58 AM
#4
davinci, thanks for your reply, i was hoping for more like an example, but thanks anyway...
-
February 26th, 2005, 07:45 AM
#5
Junior Member
hi
I was unable to give example tht time here it is:
Code:
long t;
struct tm *tp;
time(&t);
tp=localtime(&t);
cout<<"TIME IS "<<tp->tm_hour<<":"<<tp->tm_min<<":"<<tp->tm_sec<<endl;
Similarly u can add other stuff to
hoping it helps u.
-
February 26th, 2005, 06:36 PM
#6
davinci, thanks again i'll try to translate your code to C, becuase i don't know C++ thanks...
-
February 28th, 2005, 05:41 AM
#7
I really could not understand your C++ code. I would appreciate if someone has an example of how time is used in a program. I would like to see the part of your code that has time print to the screen.
-
February 28th, 2005, 06:39 AM
#8
Junior Member
kyrios,
I hav tested that program using gcc compiler
and What is the part u cant unterstand.
maybe a google search can help u.
-
February 28th, 2005, 09:00 AM
#9
Hi kyrios,
You can replace the last line of davinci's code with this line:
Code:
printf("TIME IS %d:%d:%d\n", tp->tm_hour, tp->tm_min, tp->tm_sec);
and don't forget to #include <time.h>
As for the other question, I suggest you take user's input from the arguments passed to the program.
So the user would enter:
calculate 57 * 3
And then your program replies:
171
Peace always,
<jdenny>
Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds
-
February 28th, 2005, 10:35 PM
#10
Okay Jdenny, thanks a lot for the time code. So I guess for the variables it would be
int tm_hour, tm_min, tm_sec; /*is that the way i would use the variables?*/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|