# Thread: How to make a loop

1. ## 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 ).

2. 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. Ill try, thank you.

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.

5. Originally Posted by Shurmin
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. 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.

7. how to post my program plz any body guide me

8. 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.

9. Originally Posted by iMalc
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. ## its shows the unrechable statement error

Your post moved here -> http://cboard.cprogramming.com/c-pro...able-code.html