LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2002, 10:01 AM   #1
NSKL
Senior Member
 
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207

Rep: Reputation: 47
Dumb.. but i cant figure it out.


Im learning C form some cheap book, and writing short programs just to see if i understood how the command works.
Well here is a simple program that won't compile:
# include <stdio.h>

main ()
{
char ans;

/* Get correct input fromn the user, loop until the input is correct (y or n) */

do {

printf ("Is your printer turned ON? ( 'y' or 'n' )");
scanf (" %c", ans);
while ((ans != 'y') && (ans != 'n'));
}

/* Use a conditional to tell waht is happening once the correct input was entered */

/* line18 */ (ans == 'y') ? (printf ("I will start printing now.\n")) : (printf ("Turn on the printer and run the program again please.\n"));
return 0;
}

Here is the output from the compiler:
--(Sat Nov 9, 16:56:55)--(nskl@SlackBox:~/programming)$ gcc -o dowhile dowhile.c
dowhile.c: In function `main':
dowhile.c:18: parse error before `('
--(Sat Nov 9, 16:57:04)--(nskl@SlackBox:~/programming)$


I'm thinking it must be something simple... But i tried rewriting the program, changing it around, even using two if statements instead of the conditional, but i can't get it to work. Can anyone point me to waht is wrong here??
Thank you
-NSKL

Last edited by NSKL; 11-09-2002 at 10:03 AM.
 
Old 11-09-2002, 10:20 AM   #2
Mephisto
Member
 
Registered: Oct 2002
Location: Washington D.C, USA
Distribution: Slack 12, Etch, Gutsy
Posts: 453

Rep: Reputation: 31
I haven't looked at it all but let me give you one that stands out. Then see how you go from there. look at the location of your while in relation to your do.

It is either

do
<statement>
while

or

do{
<1 or more statements>
}while
 
Old 11-09-2002, 12:02 PM   #3
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
also --and I haven't looked in a manual-- the scanf looks a little hokey. I think that it should be something more like

scanf (" %c", &ans);

alternately, you might do your decl like this

char ans[1];
 
Old 11-09-2002, 12:06 PM   #4
NSKL
Senior Member
 
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207

Original Poster
Rep: Reputation: 47
Thank you, i moved while statement out of the do loop and it works well.
My mistake.
Thanks again
-NSKL
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can anyone Figure this out? BajaNick LQ Suggestions & Feedback 17 12-16-2003 11:36 PM
I can't figure it out NightWolf_NZ Linux - Hardware 1 09-10-2003 10:08 PM
Can anyone figure this out? Nicksan Linux - General 1 08-17-2003 06:15 PM
cant figure this out. Must be m$ wlfdgcrkz Linux - Networking 1 07-16-2003 03:34 PM
how do i figure out.... wr3ck3d Linux - Hardware 0 12-23-2002 12:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration