G++ on Linux - error message - Help !

I'm using Ubuntu 12.04 and Bash.

This is what I enter:-

g++ -o Bob19 Bob19.c

and for 'Hello World' works fine !

But I came unstuck with trying to get a Do Loop to work.

tim@tim-A880G:~$ g++ -o Bob19 Bob19.c
Bob19.c: In function ‘int main()’:
Bob19.c:26:27: error: expected ‘)’ before ‘;’ token
tim@tim-A880G:~$

This is the code:-

#include <stdio.h>

int main()
{
float num1, num2, result;
char choice;


do {

printf(" Number 1 ");
scanf(" %f", &num1);

printf(" Number 2 ");
scanf(" %f", &num2);


result = num1 + num2;
printf ("Answer \n \n", result) ;

scanf(" %c", &choice);
if (choice == 'n') ;
{
choice = 'N';
}
} while (choice != 'N';

printf("Hello world - Bob19 ! \n");
return 0;

}

________________________

I'm a beginner with C and I think its about what G++ likes and doesn't like about 'main' at the start.

Any thoughts very very welcome ! (Or any example of a Do loop that actually works ! *LOL* )

A puzzled,

Tim

} while (choice != 'N';

you're missing the closing bracket after the 'N'.

Edit: by the way this:

Bob19.c:26:27: error: expected ‘)’ before ‘;’ token


is telling you what line(s) the problem is on.

edit 2: if (choice == 'n') ;

you also dont need a semi-colon there.
Last edited on
Many many thanks for your reply !

I finally got this code to compile and run OK ! A massive step forward for me !

code:-

#include <stdio.h>
main()
{
float num1, num2, result;
char choice;

do {
printf(" Number 1 ");
scanf(" %f", &num1);
printf(" Number 2 ");
scanf(" %f", &num2);

result=num1+num2;
printf ("Answer \n \n", result) ;
scanf(" %c", &choice);
if (choice == 'n') ;
{
choice = 'N';
}
} while (choice != 'N');
printf("%.2f\n\n",result);
printf("Hello world - bob22 ! \n");
return 0;

}

End Code

So you have made my day special with your help !

: ))

Tim
nice one dude.
Make sure you stick your code between code tags next time :)
I've been making progress using GCC but got tangled in my thinking of how Do Loops work,

I have inputs on two ports:-

0 0
0 1
1 0
1 1

are the possible combinations.

depending on what and when the data is I want to turn on a port or off a port. (1 or 0).

so if I start a Do Loop I want to continually read a data port and turn an output until data changes.

(I'm using the reading keyboard to set Port1 and Port2 values and the screen to receive the answer/output .)

Now a ' Do' finished with a 'while'

QUESTION:-

now can a 'while ' be associated with two 'conditional' statements ? if so what is the syntax of it ?.

if so I could run loop 1 which when a change of data occurred move to Loop 2 then to loop3 then to loop 4 and start again from the beginning.

____

I've been driving myself crackers/mad for some days , there may be an easier route but I thought I'd post this for more help.

Hope my rambling makes some sense !

Any thoughts very welcome !

Tim
Last edited on
closed account (o3hC5Di1)
Hi there,

Please make a separate topic for issues unrelated to the initial topic - it keeps the forum nice and tidy.
As for your question:

QUESTION:-

now can a 'while ' be associated with two 'conditional' statements ? if so what is the syntax of it ?.

if so I could run loop 1 which when a change of data occurred move to Loop 2 then to loop3 then to loop 4 and start again from the beginning.


Yes, you can have a while() statement with more than one condition as such:

1
2
while (letter == 'n' || letter =='N')  //using logical OR, if letter is n or N
while (number > 0 && number < 100) //using logical AND, if number is larger than 0 and smaller than 100 


I hope that's what you mean - otherwise I'm afraid I'll need you to clarify your question, perhaps with some sample code. Please do let us know if you have any further questions.

All the best,
NwN
A note:
GCC means GNU Compiler Collection. You call "g++", but there is also "gcc". gcc is the C compiler and g++ is the C++ compiler. If you write C, then it is more consistent to use the "gcc".

Both compilers have many options. It is usually good to have some warning options enabled; one can spot some potential problems with them.

Both compilers support more than one version of the languages. The default is usually not the latest, and it has "GNU extensions" enabled. The extensions add features that are not in strict standard. While convenient, their use can be a problem if one has to compile with some other compiler later. See option "-std=" from "man gcc".
Many thanks for responding !

It was the logical operators I had failed to find !

So I'm greatly relieved and very appreciative,

I did switch from g++ to gcc after receiving previous advice.

Tim
In case anyone stumbles in this Do Loop thread here is what worked ! Thanks to members of this Forum !


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

  do   {
     // stage 3   0     0        0
        output3 = 0;
      printf(" Port 1 ");
      scanf(" %f", &port1);
      printf(" Port 2 ");
      scanf(" %f", &port2);
       
      output3 = 0;
      
       printf("Answer stage 3                       ") ;   
      printf("%.2f\n",output3); 
      printf("End of Stage 3 \n ") ;
      
      }
      while (port1 == 0 && port2 == 0)  ;
      
     printf(" stage 4 next maybe\n");
         
     



The help was greatly appreciated !

Tim
Topic archived. No new replies allowed.