Results 1 to 4 of 4

Thread: Loop in C

  1. #1
    Join Date
    Mar 2011
    Beans
    87

    Loop in C

    I'm trying to get a Do Loop to work but get the error message:-

    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:~$

    part of the code is:-

    #include <stdio.h>

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


    do {

    etc....

    Total beginner so no idea why it complains

    I invoke it with:-

    g++ -o Bob19 Bob19.c


    at the bash prompt.

    Any thoughts very welcome !

    A puzzled,

    Tim
    Last edited by mörgæs; October 4th, 2013 at 10:36 AM. Reason: Thread title changed.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Programming in Bash using G-++

    It looks like C and you should compile it with gcc not g++ which is for C++
    you have a syntax error there, and as the error says in the 26th line you have some ')' missing

    it would be nice to see the code near the 26th line, otherwise you won't get a better answer
    Last edited by Vaphell; October 4th, 2013 at 10:37 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Mar 2011
    Beans
    87

    Re: Loop in C

    Many many thanks.

    I use G++ because GCC gave error messages even for Hello World but G++ worked.

    I will try again to use GCC. (update :- it worked using gcc so that is a bit plus !)

    I got it to work with this 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;

    }
    _____________

    I had left the ' ) ' of the 'while' line !

    It is very very early days in my attempt to use 'C', so your help was especially appreciated. I know the code is terrible but it compiled and ran OK and for me a massive step forward !

    So thank you again !

    Tim
    Last edited by Timmoore001; October 4th, 2013 at 11:40 AM.

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Loop in C

    Quote Originally Posted by Timmoore001 View Post
    Many many thanks.

    I use G++ because GCC gave error messages even for Hello World but G++ worked.

    I will try again to use GCC. (update :- it worked using gcc so that is a bit plus !)
    Some or the more "modern" elements of C syntax ("//"" comments, loop variable declaration in the lop condition, etc...) have been borrowed from C+, so G++, that assumes a C++ syntax, will consider them OK (but will also imply C++ semantics, that can be different), while GCC which uses a rather old standard by default will complain about much modernities. The real solution is to tell GCC to use a more recent standard (-std=c99 option).
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •