noobie loop question

This is a discussion on noobie loop question within the C++ Programming forums, part of the General Programming Boards category; i get alot of ; expected before ) or )expected before ; errors when i try to compile this code, ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    noobie loop question

    i get alot of ; expected before ) or )expected before ; errors when i try to compile this code, i am using bloodshed dev-c++ compiler. this is just me practicing what i have learnt while reading the tutorials
    Code:
     #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x,y;
      cout<<"input X\n"; 
      cin>> x;
      cout<<"x will be modified by this variable, (y)\n";
      cin>>y;
      do (
        x = x + y; /*error in this line, "expected ')' before ';' token */
        x++;
        cout<< x<<"\n";
        cin.ignore();
        )
      while (x < 10)
      cin.get();
      }

  2. #2
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Hi I think I know whats wrong with it. Change the ( after the do to { and the same thing at the other ) should be }

    Code:
     #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x,y,e;
      cout<<"input X\n"; 
      cin>> x;
      cout<<"x will be modified by this variable, (y)\n";
      cin>>y;
      do{ 
        e = x+y;
        x++;
        cout << "answer = " << e<<endl;
        }
      while (x < 10);
      cin.get();
    }
    Sorry I change the code a little^^; but its the same as yours anyways. Just change the "Do" () to {} and it should be ok.
    Last edited by adr; 12-24-2005 at 01:36 PM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    thank you! it works now, but what does endl do? i didnt understand that when the tutorial explained it, so i kinda coded around it.

  4. #4
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Its the same as
    Code:
    <<"\n";
    just spelled out. It ends the line.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    thanks again

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    But I dont think new use really need to know that yet really. Not till you use bigger programing. + Thats new to meXD I was showed to always use that befor the end if you get a int like that.
    Last edited by adr; 12-24-2005 at 01:57 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by adr
    But I dont think new use really need to know that yet really. Not till you use bigger programing. + Thats new to meXD I was showed to always use that befor the end if you get a int like that.
    Is there a bablefish that can tell me what this means?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Dave_Sinkula
    Is there a bablefish that can tell me what this means?
    Haha.

    adr, I don't see how endl is worse than "\n". endl implies.. oh I don't know, end line. What the hell is a backslash and one character imply? nothing, its only known because the newbie is taught it. Doesn't matter which is taught first really...
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    HuhO.O never said it was a bad thing. I just said that new users dont need to know that just right off yet Just makes things a little weird. I know it doesnt matter really, but just to make things a little easyer on ppl its better to do things they know how and teach them something differnt on the way:P
    Last edited by adr; 12-24-2005 at 04:20 PM.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I use endl instead of '\n' if it's after a variable or something . . . but I don't see the point in using it like this:
    Code:
    cout << "hello" << endl;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    endl inserts a newline character AND flushes the stream.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I knew someone was going to say that. Yes, if you want to flush the stream as well, then use endl.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 09:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-30-2008, 12:12 AM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-07-2006, 12:43 AM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. for loop question
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 03-20-2006, 09:42 AM

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