# How to make a loop

• 03-30-2012
Shurmin
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 ).
• 03-30-2012
überfuzz
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.
• 03-30-2012
Shurmin
Ill try, thank you.
• 03-30-2012
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)

I started reading about, and learning, C++ only 4 days ago.. For the first time in my life. So i'm not that experienced.
• 03-30-2012
überfuzz
Quote:

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?
• 03-30-2012
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.
• 03-31-2012
tauqeer ahmed
how to post my program plz any body guide me
• 03-31-2012
überfuzz
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.
• 03-31-2012
Shurmin
Quote:

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.
• 03-31-2012
tauqeer ahmed
its shows the unrechable statement error
Your post moved here -> http://cboard.cprogramming.com/c-pro...able-code.html
• 03-31-2012
iMalc
Quote:

Originally Posted by Shurmin
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.
• 03-31-2012
Salem