Thread: How to make a loop

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    How to make a loop

    Allright - I've written this code
    Code:
    #include <iostream>
    using namespace std;
    #include <string>
    int main()
    {
        float matKar, danKar, engKar;
        string navn, eNavn;
        cout << "Matematik karakter: "; cin >> matKar;
        if ( (matKar == -3) || (matKar == 0.2) || (matKar == 02) || (matKar == 4) || (matKar == 7) || (matKar == 10) || (matKar == 12) ) {
        cout << "Dansk karakter: ";
        }
        else {
            cout << "Ugyldigt tal! Skriv venligst en gyldig karakter.\n\n";
            return main();
        }
        cin >> danKar;
        if ( (danKar == -3) || (danKar == 0.2) || (danKar == 02) || (danKar == 4) || (danKar == 7) || (danKar == 10) || (danKar == 12) ) {
        cout << "Engelsk karakter: ";
        }
        else {
            cout << "Ugyldigt tal! Skriv venligst en gyldig karakter.\n\n";// Here, i want it to return to the "cin >> danKar;" - I've written it in purple so it's easier to spot
                    
        }
        cin >> engKar;
        if ( (engKar == -3) || (engKar == 0.2) || (engKar == 02) || (engKar == 4) || (engKar == 7) || (engKar == 10) || (engKar == 12) ) {
    
    
        cout << "\nFornavn og Efternavn: "; cin >> navn >> eNavn;
        cout << "--------------------------------------------------------------------------------" << endl;
        cout << "Karakter gennemsnit for " << navn << " " << eNavn;
        cout << " er " << ( matKar + danKar + engKar ) / 3 << "\n \n";
        }
        else {
            cout << "Ugyldigt tal! Skriv venligst en gyldig karakter.\n\n";// Here, i want it to go back to the "cin >> engKar;" - I've written it in red so it's easier to spot.
            
        }
        return 0;
    }


    Don't worry about the language, it's danish, and no need to understand it.

    Thanks in advance for any help, i really appreciate it if you took the time to help me out. - If it's somehow complicated because of how i've written it, please do come with suggestions on how i could write it instead - Giving the same result, but easier ( It's not hard for me to understand, but maybe that's cause i know what i've written ).
    Last edited by Shurmin; 03-30-2012 at 01:20 PM.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Hmm., just an idea... Is it possible for you to make functions such as engKar(), danKar() etc. Then if you need to jump to engKar you just run engKar from where you need to jump.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Ill try, thank you.
    Last edited by Shurmin; 03-30-2012 at 01:34 PM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    I've tried, but i simply cannot make it work..

    Could you write in code exactly what it is you mean? (just copy paste mine, and put in what you think i should put in)

    I started reading about, and learning, C++ only 4 days ago.. For the first time in my life. So i'm not that experienced.
    Last edited by Shurmin; 03-30-2012 at 01:44 PM.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Quote Originally Posted by Shurmin View Post
    I've tried, but i simply cannot make it work..

    Could you write in code exactly what it is you mean? (just copy paste mine, and put in what you think i should put in)
    Na... I think that would spoil your fun, and your homework. Don't you think?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You will have a big problem trying to compare the float value against the double of 0.2. You should find that it will not ever come out equal.
    Search for and read "What every computer scientist should know about floating point".

    Secondly, be very careful placing a leading zero in front of a number, that turns it from base 10 to base 8.

    You're doing the exact same thing three times (checking against the same list of values). You should therefore make that into a function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    how to post my program plz any body guide me

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Tanqueer Ahmed - First of all. It's often perceived as bad forum manners to hijack threads.

    You have got an account so you can post in threads, edit your posts and start new threads. What you need to do is locate the button that says [+ Post New Thread ] click it and you'll be halfway there.
    Last edited by überfuzz; 03-31-2012 at 04:46 AM.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Quote Originally Posted by iMalc View Post
    You will have a big problem trying to compare the float value against the double of 0.2. You should find that it will not ever come out equal.
    Search for and read "What every computer scientist should know about floating point".

    Secondly, be very careful placing a leading zero in front of a number, that turns it from base 10 to base 8.

    You're doing the exact same thing three times (checking against the same list of values). You should therefore make that into a function.
    well, could you please enlighten me? I'm not experienced enough to know what to do here, and if you could write in code just an example, please do. -- This is getting stressing =/

    Thanks though, for the quick replies, and the attempt to help.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    its shows the unrechable statement error

    Your post moved here -> unreachable code
    Please stop hijacking someone else's thread with your question.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Shurmin View Post
    well, could you please enlighten me? I'm not experienced enough to know what to do here, and if you could write in code just an example, please do. -- This is getting stressing =/

    Thanks though, for the quick replies, and the attempt to help.
    Which part of my post needs explaining?
    I would assume you searched for and read the article (what every computer scientist should know about floating point - Bing), know or can look up what base 10 and base 8 are, and are capable of searching for a function tutorial.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    @Carl (another thread hijacker - WTF) your post is here -> Help Needed please!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i make this loop???
    By lancehumiston in forum C Programming
    Replies: 8
    Last Post: 04-11-2011, 05:11 PM
  2. For loop to make patterns
    By zfite in forum C Programming
    Replies: 18
    Last Post: 03-08-2011, 02:26 PM
  3. how would you make a for loop infinite?
    By muzihc in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 03:57 AM
  4. Help to make a loop.
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2008, 06:25 AM
  5. Using a for loop to make a diamond...
    By wco5002 in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2007, 08:56 PM