Very Simple Problem - NEED HELP PLEASE
Author |
Message |
Fred Mori #1 / 15
|
 Very Simple Problem - NEED HELP PLEASE
I have a couple of question. First I need to write a question that takes three elements 2 are float, the last is char The question is Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) I write Latitude: ; puts("\Enter the....:"); scanf("%f%f%s", °res, &minutes, pole); /* next i wanna validate the entries so I write */ if (degres < 0 || degres > 90) Goto Latitude; else if (minutes < 0 || minutes > 60) Goto Latitude; else if (pole != 'N' || pole != 'S') goto latitude; .... /* Obviously my problem is with the third validation. I know it`s wrong but i don`t know how to fix it. */ Second Question. What is the proper way of printing a float without decimals. Thank You
|
Sat, 20 Mar 1999 03:00:00 GMT |
|
 |
Ken Nicols #2 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
>I have a couple of question. >First >I need to write a question that takes three elements >2 are float, the last is char >The question is >Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) >I write >Latitude: ; >puts("\Enter the....:");
"\E" is undefined - I'll assume you meant "\nE..." Quote: >scanf("%f%f%s", °res, &minutes, pole);
It would be nice if we could see the declarations of these variables. scanf is pretty bad for parsing user input, but we'll skip over that for now. Quote: >/* next i wanna validate the entries so I write */
^^^^^ "want to" is correct English. Quote: >if (degres < 0 || degres > 90) >Goto Latitude; >else >if (minutes < 0 || minutes > 60) >Goto Latitude; >else >if (pole != 'N' || pole != 'S') >goto latitude;
Your compiler seems to have a very lax attitude towards capitalisation. Unlike BASIC, which this code very strongly reminds me of, C is case-sensitive. Quote: >/* Obviously my problem is with the third validation. I know it`s >wrong but i don`t know how to fix it. */
assuming you have: char pole[ENOUGH_SPACE]; somewhere, then; if ( strcmp( pole, "N" ) && strcmp( pole, "S" ) ) should do, or you could declare "pole" as a simple char, but you would need to change the scanf(). You should (must?) rewrite the the code to drop these gotos. For simple validation like this, if {} else {} blocks will suffice and be much easier to maintain at a later date. Also, please post a complete sample, not just snippets, as we have to try to guess at declarations and such like. Quote: >Second Question. >What is the proper way of printing a float without decimals.
printf( "%.0f", 123.456 ); Quote: >Thank You
Please also pick up the FAQ, it's essential reading, and can be found on the web at http://www.eskimo.com/~scs/C-faq/top.html Ken -- ___ ,_, ___ Ken Nicolson (o,o) (o,o) ,,,(o,o),,,
-"-"- -"-"- -"-"- +44 131 555 6055
hoots about my opinions Web: http://www.ride.demon.co.uk
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
Robert B. Rossman #3 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> I have a couple of question. > First > I need to write a question that takes three elements > 2 are float, the last is char > The question is > Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) > Latitude: ; > puts("\Enter the....:"); > scanf("%f%f%s", °res, &minutes, pole); > if (degres < 0 || degres > 90) > Goto Latitude; > else > if (minutes < 0 || minutes > 60) > Goto Latitude; > else > if (pole != 'N' || pole != 'S') > goto latitude; > Second Question. > What is the proper way of printing a float without decimals.
First question: pole is a string. So use if ( strcmp (pole,"N") && strcmp (pole, "S")) or if pole is a char there are 2 mistakes: 1.) scanf ("%f%f%c",°res,&minutes,&pole); . . 2.) if (pole != 'N' && pole != 'S') Second question: printf ("%.0f",1.23456) will print 1 ------------------------------------------------------------------------Robert
Institut fuer Theoretische Physik|Phone: +43 (0316) 380 5242 Universitaetsplatz 5 |WWW : http://physik.kfunigraz.ac.at/~rbr/ 8010 Graz | Austria (Europe) |The Amiable Dragon-==(UDIC)==- ------------------------------------------------------------------------
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
Robert B. Rossman #4 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> I have a couple of question. > First > I need to write a question that takes three elements > 2 are float, the last is char > The question is > Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) > Latitude: ; > puts("\Enter the....:"); > scanf("%f%f%s", °res, &minutes, pole); > if (degres < 0 || degres > 90) > Goto Latitude; > else > if (minutes < 0 || minutes > 60) > Goto Latitude; > else > if (pole != 'N' || pole != 'S') > goto latitude; > Second Question. > What is the proper way of printing a float without decimals.
First question: pole is a string. So use if ( strcmp (pole,"N") && strcmp (pole, "S")) or if pole is a char there are 2 mistakes: 1.) scanf ("%f%f%c",°res,&minutes,&pole); . . 2.) if (pole != 'N' && pole != 'S') Second question: printf ("%.0f",1.23456) will print 1 ------------------------------------------------------------------------Robert
Institut fuer Theoretische Physik|Phone: +43 (0316) 380 5242 Universitaetsplatz 5 |WWW : http://physik.kfunigraz.ac.at/~rbr/ 8010 Graz | Austria (Europe) |The Amiable Dragon-==(UDIC)==- ------------------------------------------------------------------------
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
Robert B. Rossman #5 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> I have a couple of question. > First > I need to write a question that takes three elements > 2 are float, the last is char > The question is > Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) > Latitude: ; > puts("\Enter the....:"); > scanf("%f%f%s", °res, &minutes, pole); > if (degres < 0 || degres > 90) > Goto Latitude; > else > if (minutes < 0 || minutes > 60) > Goto Latitude; > else > if (pole != 'N' || pole != 'S') > goto latitude; > Second Question. > What is the proper way of printing a float without decimals.
First question: pole is a string. So use if ( strcmp (pole,"N") && strcmp (pole, "S")) or if pole is a char there are 2 mistakes: 1.) scanf ("%f%f%c",°res,&minutes,&pole); . . 2.) if (pole != 'N' && pole != 'S') Second question: printf ("%.0f",1.23456) will print 1 ------------------------------------------------------------------------Robert
Institut fuer Theoretische Physik|Phone: +43 (0316) 380 5242 Universitaetsplatz 5 |WWW : http://physik.kfunigraz.ac.at/~rbr/ 8010 Graz | Austria (Europe) |The Amiable Dragon-==(UDIC)==- ------------------------------------------------------------------------
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
Ulrich Gehler #6 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> I have a couple of question. > First > I need to write a question that takes three elements > 2 are float, the last is char > The question is > Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) > I write > Latitude: ; > puts("\Enter the....:");
\E is not a valid escape. Quote: > scanf("%f%f%s", °res, &minutes, pole); > /* next i wanna validate the entries so I write */ > if (degres < 0 || degres > 90) > Goto Latitude; > else > if (minutes < 0 || minutes > 60) > Goto Latitude; > else > if (pole != 'N' || pole != 'S') > goto latitude; > .... > /* Obviously my problem is with the third validation. I know it`s > wrong but i don`t know how to fix it. */
Say: if (pole[0] != 'N' || pole[0] != 'S') or: if (*pole != 'N' || *pole != 'S') Or, make `pole' a char instead of a char[] and scan it with %c. And for God's sake, get rid of these goto's. Like this: int bad_input; do { /* Get input. */ /* ... */ /* Now check it. */ bad_input = (degrees < 0 || degrees > 90) || (minutes < 0 || minutes > 60) || (pole[0] != 'N' || pole[0] != 'S'); Quote: }
while (bad_input); Quote: > Second Question. > What is the proper way of printing a float without decimals.
printf("%1.0f", 3.14159) /* prints '3' */ printf("%1.0f", 2.71821) /* also prints '3', because of rounding */ See also the `floor' function. --
IBM European Networking Center . . . VNET: GEHLERT at MAZVM01 Telecooperation Department . . . . Phone: +49-6221-59-4429 (FAX -3300) Vangerowstr. 18 . . . . . . . . . . . ITN: *142-4429 D-69118 Heidelberg Quote: >>> Opinions are my own, not those of IBM. <<<
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
James Youngm #7 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote: >if (pole != 'N' || pole != 'S') >/* Obviously my problem is with the third validation. I know it`s >wrong but i don`t know how to fix it. */
if (pole[0] != 'N' || pole[0] != 'S') is a start. You need to get clear the difference between a "char*" and a "char". -- James Youngman VG Gas Analysis Systems |The trouble with the rat-race Before sending advertising material, read |is, even if you win, you're http://www.law.cornell.edu/uscode/47/227.html|still a rat.
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
John Hobso #8 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> I have a couple of question. > First > I need to write a question that takes three elements > 2 are float, the last is char > The question is > Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) > I write > Latitude: ; > puts("\Enter the....:"); > scanf("%f%f%s", °res, &minutes, pole); > /* next i wanna validate the entries so I write */ > if (degres < 0 || degres > 90) > Goto Latitude; > else > if (minutes < 0 || minutes > 60) > Goto Latitude; > else > if (pole != 'N' || pole != 'S') > goto latitude; > .... > /* Obviously my problem is with the third validation. I know it`s > wrong but i don`t know how to fix it. */
Another victim of DeMorgan's Laws. You want if (pole != 'N' && pole != 'S') Quote: > Second Question. > What is the proper way of printing a float without decimals.
I'm not sure that I understand the question. Do you want to simply omit the decimal point, printing "12.34" as "1234" or as "12" (omitting the decimal part.) PS. Do not put "same" in your Reply-To field. Put in your actual e-mail address. Netscape barfed on "same". -- John Hobson |Whenever someone says to me, Unix Support Group |"Have a nice day", I reply, ComEd, Chicago, IL, USA |"Sorry, I've made other plans."
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
John Hobso #9 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> I have a couple of question. > First > I need to write a question that takes three elements > 2 are float, the last is char > The question is > Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) > I write > Latitude: ; > puts("\Enter the....:"); > scanf("%f%f%s", °res, &minutes, pole); > /* next i wanna validate the entries so I write */ > if (degres < 0 || degres > 90) > Goto Latitude; > else > if (minutes < 0 || minutes > 60) > Goto Latitude; > else > if (pole != 'N' || pole != 'S') > goto latitude; > .... > /* Obviously my problem is with the third validation. I know it`s > wrong but i don`t know how to fix it. */
Another victim of DeMorgan's Laws. You want if (pole != 'N' && pole != 'S') Quote: > Second Question. > What is the proper way of printing a float without decimals.
I'm not sure that I understand the question. Do you want to simply omit the decimal point, printing "12.34" as "1234" or as "12" (omitting the decimal part.) -- John Hobson |Whenever someone says to me, Unix Support Group |"Have a nice day", I reply, ComEd, Chicago, IL, USA |"Sorry, I've made other plans."
|
Sun, 21 Mar 1999 03:00:00 GMT |
|
 |
Al Bower #10 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> > I have a couple of question. > > First > > I need to write a question that takes three elements > > 2 are float, the last is char > > The question is > > Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) > > Latitude: ; > > puts("\Enter the....:"); > > scanf("%f%f%s", °res, &minutes, pole); > > if (degres < 0 || degres > 90) > > Goto Latitude; > > else > > if (minutes < 0 || minutes > 60) > > Goto Latitude; > > else > > if (pole != 'N' || pole != 'S') > > goto latitude; > > Second Question. > > What is the proper way of printing a float without decimals. > First question: pole is a string. So use > if ( strcmp (pole,"N") && strcmp (pole, "S")) > or if pole is a char there are 2 mistakes: > 1.) scanf ("%f%f%c",°res,&minutes,&pole); > . > . > 2.) if (pole != 'N' && pole != 'S')
As precaution in that the user might enter the pole character as lowercase, I would convert to upper case with function toupper. if(toupper(pole) != 'N' || toupper(pole) != 'S') -- Al Bowers Tampa, FL
http:www.gate.net/~abowers/index.html
|
Tue, 23 Mar 1999 03:00:00 GMT |
|
 |
Adam Bold #11 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
>I have a couple of question. >First >I need to write a question that takes three elements >2 are float, the last is char >The question is >Enter the latitude of the city by degresm minutes and pole (Ex: 45 30 N) >I write >Latitude: ; >puts("\Enter the....:"); >scanf("%f%f%s", °res, &minutes, pole);
Where is the & before pole? suggestion > scanf("%f%f%s", °res, &minutes, &pole); Quote: >/* next i wanna validate the entries so I write */ >if (pole != 'N' || pole != 'S') >goto latitude; >.... >/* Obviously my problem is with the third validation. I know it`s >wrong but i don`t know how to fix it. */ >Second Question. >What is the proper way of printing a float without decimals.
You may want to check on this, I can't remember too well. printf ("Latitude %.0f", °res); Or something close to this should work. Quote: >Thank You
Your welcome, Adam Bolduc
|
Fri, 26 Mar 1999 03:00:00 GMT |
|
 |
Tanmoy Bhattachary #12 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> >puts("\Enter the....:"); > >scanf("%f%f%s", °res, &minutes, pole); > Where is the & before pole? > suggestion > scanf("%f%f%s", °res, &minutes, &pole);
Huh? What did you think pole was declared as? Cheers Tanmoy --
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544 Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>, <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/ internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html> fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
|
Sun, 28 Mar 1999 03:00:00 GMT |
|
 |
Michael A. Crowde #13 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> (snip) > puts("\Enter the....:"); > scanf("%f%f%s", °res, &minutes, pole); > (snip) > if (pole != 'N' || pole != 'S') > (snip) > /* Obviously my problem is with the third validation. I know it`s > wrong but i don`t know how to fix it. */ > (snip)
You have not indicated how pole is declaired. Is is something like char pole[2]; or char pole[10]; ? In any case, pole must be a pointer to char. In your 'if' statement, you are comparing a pointer to char with a character. This will always fail. Apples and Oranges! Try this: if (pole[0] != 'N' ... or this: if (*pole != 'N' ... By the way, if pole is declaired as char pole[2]; and your user types 45 30 North, six bytes will be stored in pole (space was reserved for only 2 bytes (don't forget the null at the end)).
|
Mon, 29 Mar 1999 03:00:00 GMT |
|
 |
Erick Wagne #14 / 15
|
 Very Simple Problem - NEED HELP PLEASE
|> |> (snip) |> puts("\Enter the....:"); |> scanf("%f%f%s", °res, &minutes, pole); |> (snip) |> if (pole != 'N' || pole != 'S') |> (snip) |> /* Obviously my problem is with the third validation. I know it`s |> wrong but i don`t know how to fix it. */ |> |> (snip) | |You have not indicated how pole is declaired. Is is something |like char pole[2]; or char pole[10]; ? In any case, pole must be |a pointer to char. In your 'if' statement, you are comparing a |pointer to char with a character. This will always fail. Apples and |Oranges! Try this: if (pole[0] != 'N' ... or this: if (*pole != 'N' ... Also... do you always know that your input will be in uppercase? I'd suggest using toupper(pole[0]) or toupper(*pole) before comparing against those uppercase literals
|
Mon, 29 Mar 1999 03:00:00 GMT |
|
 |
Mike Rubenste #15 / 15
|
 Very Simple Problem - NEED HELP PLEASE
Quote:
> |> > |> (snip) > |> puts("\Enter the....:"); > |> scanf("%f%f%s", °res, &minutes, pole); > |> (snip) > |> if (pole != 'N' || pole != 'S') > |> (snip) > |> /* Obviously my problem is with the third validation. I know it`s > |> wrong but i don`t know how to fix it. */ > |> > |> (snip) > | > |You have not indicated how pole is declaired. Is is something > |like char pole[2]; or char pole[10]; ? In any case, pole must be > |a pointer to char. In your 'if' statement, you are comparing a > |pointer to char with a character. This will always fail. Apples and > |Oranges! Try this: if (pole[0] != 'N' ... or this: if (*pole != 'N' ... > Also... do you always know that your input will be in uppercase? > I'd suggest using toupper(pole[0]) or toupper(*pole) before comparing > against those uppercase literals
Or, better, do it right and use toupper((unsigned char) pole[0]) or toupper((unsigned char) *pole). Never use a char variable as an argument for toupper (or any of the other functions in <ctype.h>. If char is signed and the value is negative, you may be passing an invalid argument. toupper() (and the other <ctype.h> functions) requires an int argument that contains a value that can be represented as unsigned char or EOF. Michael M Rubenstein
|
Mon, 29 Mar 1999 03:00:00 GMT |
|
|
|