Thread: Never programmed before, and got a problem.

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    8

    Question Never programmed before, and got a problem.

    I actually just started trying the C++ tutorials on the main site last night, and I'm now completely and utterly engrossed. I don't know why I suddenly decided I wanted to learn a programming language, but anyway, that's not important.

    I've hit a snag somewhere, well, it's probably something really simple that I'm too inexperienced to notice. I'm only on lesson 3, loops. But when I try the codes in this lesson, they don't work. I type them, letter for letter, compile and then run, and don't get the expected results, but just 0s trailing down the screen. I mess around a bit, in case I made a mistake, and that doesn't help. I don't know anyone else who knows much about C++, the only person I do know is about 6 miles away and doesn't have a house phone.

    I'm actually in the dark with the compiler, too. Nowhere can I find any information on how to use it, so I could be doing something wrong there, too.

    Anyway, although you can find the code in the lesson 3 tutorial, I'll just add what I've got down. Maybe someone could spot where I've gone wrong.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
        
        x = 0;
        do {
            cout<<"Good morning maddam!\n";
        } while ( x != 0 );
        cin.get();
    }
    According to the tutorial the text should be printed more than once, but as I said, all I get is lots of 0s. If anyone can help, I'd be very greatful.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The code looks okay to me.

    I'm only on lesson 3, loops. But when I try the codes in this lesson, they don't work. I type them, letter for letter, compile and then run, and don't get the expected results, but just 0s trailing down the screen.
    Do the code examples in the previous lessons work for you?

    I'm actually in the dark with the compiler, too. Nowhere can I find any information on how to use it, so I could be doing something wrong there, too.
    What compiler are you using, and how exactly are you using it?

    According to the tutorial the text should be printed more than once
    Actually, according to the tutorial "the block will be executed at least once", and indeed the text is printed exactly once.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Yeah, so you probably have a good idea of exactly how clueless I am now. I thought that's what executed meant.

    Well, yes, previous lessons worked perfectly, after I sorted the odd mistakes and typos.

    The compiler is... lemmie check... "Dev-Cpp", Both my mum's partner and my boyfriend recommended I use it because apparently it's easy. Apparently. I create a new source file, type in the code directly, save, compile, then run. That's the only use I've made of it so far. When I compiled there were no errors and no mistakes were indicated.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Apparently. I create a new source file, type in the code directly, save, compile, then run. That's the only use I've made of it so far. When I compiled there were no errors and no mistakes were indicated.
    I do not know why you get those "0s trailing down the screen" (a test with Dev-C++ gives no such problem for me), but one suggestion that may or may not help you is to create a project and have a source file within that project, instead of using Dev-C++ with a source file rightaway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Oh, well. That didn't work. In fact, it didn't even run at all. Thanks for the suggestion though.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    The program ran fine for me but all it did was print "good morning maddam" once then awaited keypress to exit the program. The do-while loop is not needed. If you wanted the string to print a set amount of times you could do this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
        
        x = 0;
        do {
            cout<<"Good morning maddam!\n";
            x++;
        } while ( x < 10 );
        cin.get();
    }
    If you give the loop condtion it can test against, then increment it untill it reaches the desired amount, it works as a loop should work. I did not get the trailing zeros either, not sure what caused that.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> all I get is lots of 0s
    Are you leaning on the 0 key?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by swgh
    The program ran fine for me but all it did was print "good morning maddam" once then awaited keypress to exit the program. The do-while loop is not needed. If you wanted the string to print a set amount of times you could do this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
        
        x = 0;
        do {
            cout<<"Good morning maddam!\n";
            x++;
        } while ( x < 10 );
        cin.get();
    }
    If you give the loop condtion it can test against, then increment it untill it reaches the desired amount, it works as a loop should work. I did not get the trailing zeros either, not sure what caused that.
    The OP got the code from the FAQ. http://www.cprogramming.com/tutorial/lesson3.html

    If you have more than one source file open Dev-C++ can get confused I've heard. Try closing any other source files you have open.
    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.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    1. I'm not leaning on the 0 key ^_^

    2. I don't have any other source files open.

    This is quite strange. Hmm..... Y'know, I'll just carry on with the lessons, and I'll definately know there's a problem if they don't work either.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    *groan* Yes, I tried running the code in lesson 4. All I got was a blank screen inside the window. No text displayed at all.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int y;
      
      cout<<"Please input two numbers to be multiplied: ";
      cin>> x >> y;
      cin.ignore();
      cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
      cin.get();
    }
    
    int mult ( int x, int y )
    {
        return x * y;
    }

    I just can't think what I'm doing wrong.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What happens if you use the code from one of the earlier lessons that worked for you and type that code into your current project?

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    I haven't been using projects, didn't think I needed to. Do I?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I haven't used Dev-C++ much. When I did, I created a project and added a cpp file to it so that when I build the project the program is created from the source code in that file. I don't know if that is required or not.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    Not for the lessons, I'm sure. Thanks for the suggestion tho.

  15. #15
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Dribbler
    Not for the lessons, I'm sure. Thanks for the suggestion tho.
    Ive been using Dev-C++ for some time now and i always create a new project. Sometimes it sticks with the old source files even though you have created a new one in the program, so when you hit compile and run, it's the old cpp file that get compiled.

    I suggest you start from scratch and create a new project, remember to check of the radio button that says 'C++' and just pick "Console Program"

Popular pages Recent additions subscribe to a feed