Faulty Repeat
Feb 21, 2016 at 1:35pm UTC
Hi, I am trying to teach myself C++ and its not going as well as I had hoped. This program is supposed to repeat when the user wants it to. In Pascal I used to just use the REPEAT UNTIL loop, but c++ doesn't have that.
On the first pass the program runs fine, then, when the user decides to repeat it just goes haywire, flickering text ect. Is there something obvious I am doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream.h>
#include <string.h>
#include <math.h>
using namespace std;
void skuinsSy();
void oppervlak();
void repeatfunc();
int main()
{
int repeat;
do
{
system("CLS" );
int keuse;
cout << "Driehoek Bewerkings vir driehoeke met 'n 90 grade hoek: \n" << endl;
cout << "1. Lengte van skuinssy \n" ;
cout << "2. Oppervlak \n" << endl;
cout << "Watter Bewerking wil jy doen?: " ;
cin >> keuse;
if (keuse == 1)
{
skuinsSy ();
repeatfunc ();
}
if (keuse == 2)
{
oppervlak ();
repeatfunc ();
}
}
while (repeat > 0);
return 0;
}
void skuinsSy()
{
system("CLS" );
int driehoekSy1, driehoekSy2;
float driehoekSySkuins;
cout << "Bewerking van Skuinssy se lengte: \n" << endl;
cout << "Lengte van die eerste reguit sy van die driehoek?: " ;
cin >> driehoekSy1;
cout << "Lengte van die tweede reguit sy van die driehoek?: " ;
cin >> driehoekSy2;
driehoekSySkuins = sqrt((driehoekSy1 * driehoekSy1)+(driehoekSy2 * driehoekSy2));
cout << "Die lengte van die skuinssy van die driehoek is: " << driehoekSySkuins << "\n" << endl;
}
void oppervlak()
{
system("CLS" );
int basis, lhoogte;
float dhoppervlak;
cout << "Bewerking van driehoek se oppervlak: \n" << endl;
cout << "Lengte van die basis van die driehoek?: " ;
cin >> basis;
cout << "Loodregte hoogte van die driehoek?: " ;
cin >> lhoogte;
dhoppervlak = (basis * 0.5) * lhoogte;
cout << "Die oppervlak van die driehoek is: " << dhoppervlak << "\n" << endl;
}
void repeatfunc()
{
int repeat = 1;
cout << "Wil jy nog 'n bewerking doen?(y/n): " ;
cin >> repeat;
if (repeat == 'y' )
{
repeat = 1;
}
else
{
repeat = 0;
}
}
Last edited on Feb 21, 2016 at 1:41pm UTC
Feb 21, 2016 at 1:49pm UTC
In main, you create a variable called repeat. In the while loop, you are checking if that one is bigger than 0. Which it is not because you never initialized it and it has a garbage value.
What you want to do is call repeatfunc and let it decide. You can do this in 2 ways.
1. Create the repeat variable in main and pass it as reference to the function.
2. Create the repeat variable in the function itself and return it to main.
Another problem in the function. You're checking if repeat is equal to 'y'. It will never be equal to 'y', because repeat is an integer, 'y' is not an integer.
Hopefully this will give you a decent idea of the problem =)
Feb 21, 2016 at 2:09pm UTC
Ok, I solved the Problem, though I suspect there is a smarter more efficient way. Anyway, here is the changed program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream.h>
#include <string.h>
#include <math.h>
using namespace std;
void skuinsSy();
void oppervlak();
void repeatfunc();
char repeat;
int main()
{
do
{
system("CLS" );
int keuse;
cout << "Driehoek Bewerkings vir driehoeke met 'n 90 grade hoek: \n" << endl;
cout << "1. Lengte van skuinssy \n" ;
cout << "2. Oppervlak \n" << endl;
cout << "Watter Bewerking wil jy doen?: " ;
cin >> keuse;
if (keuse == 1)
{
skuinsSy ();
repeatfunc ();
}
if (keuse == 2)
{
oppervlak ();
repeatfunc ();
}
}
while (repeat == 'y' );
return 0;
}
void skuinsSy()
{
system("CLS" );
int driehoekSy1, driehoekSy2;
float driehoekSySkuins;
cout << "Bewerking van Skuinssy se lengte: \n" << endl;
cout << "Lengte van die eerste reguit sy van die driehoek?: " ;
cin >> driehoekSy1;
cout << "Lengte van die tweede reguit sy van die driehoek?: " ;
cin >> driehoekSy2;
driehoekSySkuins = sqrt((driehoekSy1 * driehoekSy1)+(driehoekSy2 * driehoekSy2));
cout << "Die lengte van die skuinssy van die driehoek is: " << driehoekSySkuins << "\n" << endl;
}
void oppervlak()
{
system("CLS" );
int basis, lhoogte;
float dhoppervlak;
cout << "Bewerking van driehoek se oppervlak: \n" << endl;
cout << "Lengte van die basis van die driehoek?: " ;
cin >> basis;
cout << "Loodregte hoogte van die driehoek?: " ;
cin >> lhoogte;
dhoppervlak = (basis * 0.5) * lhoogte;
cout << "Die oppervlak van die driehoek is: " << dhoppervlak << "\n" << endl;
}
void repeatfunc()
{
cout << "Wil jy nog 'n bewerking doen?(y/n): " ;
cin >> repeat;
}
Feb 21, 2016 at 2:10pm UTC
though I suspect there is a smarter more efficient way.
Yes, your solution is highly unrecommended. Don't fall back to global variables because it is "easier" in this situation, take your time and do it the proper way, like one of the two ways I suggested.
Feb 21, 2016 at 2:13pm UTC
Yes, your solution is highly unrecommended. Don't fall back to global variables because it is "easier" in this situation, take your time and do it the proper way, like one of the two ways I suggested.
The thing is, I don't have any handbooks or teachers and most of the examples given are a bit to complex for me to figure out exactly what was done and more importantly why it was done. Thanks for your help in any case. I will try and figure it out :)
Last edited on Feb 21, 2016 at 2:14pm UTC
Feb 21, 2016 at 3:13pm UTC
The only way I could loop the program successfully without using a global variable was to remove the repeatfunc function. I would realy appreciate a simple example of Creating a variable in a function itself and return it to main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream.h>
#include <string.h>
#include <math.h>
using namespace std;
void skuinsSy();
void oppervlak();
int main()
{
char repeat;
do
{
system("CLS" );
int keuse;
cout << "Driehoek Bewerkings vir driehoeke met 'n 90 grade hoek: \n" << endl;
cout << "1. Lengte van skuinssy \n" ;
cout << "2. Oppervlak \n" << endl;
cout << "Watter Bewerking wil jy doen?: " ;
cin >> keuse;
if (keuse == 1)
{
skuinsSy ();
cout << "Wil jy nog 'n bewerking doen?(y/n): " ;
cin >> repeat;
}
if (keuse == 2)
{
oppervlak ();
cout << "Wil jy nog 'n bewerking doen?(y/n): " ;
cin >> repeat;
}
}
while (repeat == 'y' );
return 0;
}
void skuinsSy()
{
system("CLS" );
int driehoekSy1, driehoekSy2;
float driehoekSySkuins;
cout << "Bewerking van Skuinssy se lengte: \n" << endl;
cout << "Lengte van die eerste reguit sy van die driehoek?: " ;
cin >> driehoekSy1;
cout << "Lengte van die tweede reguit sy van die driehoek?: " ;
cin >> driehoekSy2;
driehoekSySkuins = sqrt((driehoekSy1 * driehoekSy1)+(driehoekSy2 * driehoekSy2));
cout << "Die lengte van die skuinssy van die driehoek is: " << driehoekSySkuins << "\n" << endl;
}
void oppervlak()
{
system("CLS" );
int basis, lhoogte;
float dhoppervlak;
cout << "Bewerking van driehoek se oppervlak: \n" << endl;
cout << "Lengte van die basis van die driehoek?: " ;
cin >> basis;
cout << "Loodregte hoogte van die driehoek?: " ;
cin >> lhoogte;
dhoppervlak = (basis * 0.5) * lhoogte;
cout << "Die oppervlak van die driehoek is: " << dhoppervlak << "\n" << endl;
}
Feb 21, 2016 at 3:22pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
using namespace std;
int repeatFunction()
{
int repeat;
cout << "Wanna repeat? Enter 1 for Yes and 0 for No: " ;
cin >> repeat;
return repeat;
}
int main()
{
int repeat = repeatFunction(); // call the function and save the return in this variable repeat
if (repeat == 1)
{
cout << "Looks like you wanna repeat, wise choice" << endl;
}
else
{
cout << "Poor choice soldiers, now prepare to die" << endl;
}
return (0);
}
Feb 21, 2016 at 3:39pm UTC
Thank you!! I have downloaded some exercises for beginners to work through.
Thanks again.
Feb 22, 2016 at 6:16pm UTC
So I solved the problem after watching your suggested tutorial. Thanks Again for your help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include <iostream.h>
#include <string.h>
#include <math.h>
using namespace std;
void skuinsSy();
void oppervlak();
void repeatfunc(char &);
int main()
{
char repeat;
do
{
system("CLS" );
int keuse;
cout << "Driehoek Bewerkings vir driehoeke met 'n 90 grade hoek: \n" << endl;
cout << "1. Lengte van skuinssy \n" ;
cout << "2. Oppervlak \n" << endl;
cout << "Watter Bewerking wil jy doen?: " ;
cin >> keuse;
if (keuse == 1)
{
skuinsSy ();
repeatfunc(repeat);
}
if (keuse == 2)
{
oppervlak ();
repeatfunc(repeat);
}
}
while (repeat == 'y' );
return 0;
}
void skuinsSy()
{
system("CLS" );
int driehoekSy1, driehoekSy2;
float driehoekSySkuins;
cout << "Bewerking van Skuinssy se lengte: \n" << endl;
cout << "Lengte van die eerste reguit sy van die driehoek?: " ;
cin >> driehoekSy1;
cout << "Lengte van die tweede reguit sy van die driehoek?: " ;
cin >> driehoekSy2;
driehoekSySkuins = sqrt((driehoekSy1 * driehoekSy1)+(driehoekSy2 * driehoekSy2));
cout << "Die lengte van die skuinssy van die driehoek is: " << driehoekSySkuins << "\n" << endl;
}
void oppervlak()
{
system("CLS" );
int basis, lhoogte;
float dhoppervlak;
cout << "Bewerking van driehoek se oppervlak: \n" << endl;
cout << "Lengte van die basis van die driehoek?: " ;
cin >> basis;
cout << "Loodregte hoogte van die driehoek?: " ;
cin >> lhoogte;
dhoppervlak = (basis * 0.5) * lhoogte;
cout << "Die oppervlak van die driehoek is: " << dhoppervlak << "\n" << endl;
}
void repeatfunc(char & repeat)
{
cout << "Wil jy nog 'n bewerking doen?(y/n): " ;
cin >> repeat;
}
Topic archived. No new replies allowed.