-
my program does nothing
i wrote a program that plays this game we do in math class. you are given five numbers, say 5 7 3 9 and with thoes number you must get to a fith number say 11. so you think about it and say 7+3=10, 10/5=2, 2+9=11. so i wrote this program with a 3 funtions i made. i taught myself c++ over the summer and it is my first language so im not that good. any way it seemed that my computer skiped over the funtions. just ignored them. a simple example of what i was doing.
Code:
main(){
int c;
funtion();
cout<<c<<endl;
}
funtion(){
c=1;
return();
}
when i should have been doing this
Code:
main(){
int c;
c=funtion();
}
funtion(){return 1;
}
so i fixed this but now i dont get any out put at the end i looked for bout 3 hours for something wrong i cant find anything. could some one please help me. thank you very much. and here is my code
Code:
#include <iostream>
#include <math.h>
using namespace std;
float comp (float a, float b, int t);
int perm (int varo, int ea, int eb, int ec, int ed);
int computation (float sa, float sb, float sc, float sd, float se);
int main(){
float na;
float nb;
float nc;
float nd;
float ne;
int k;
int j;
int l;
int m;
int chance;
int ea = 0;
int eb = 0;
int ec = 0;
int ed = 0;
float pa = 0;
float pb = 0;
float pc = 0;
float pd = 0;
cout<<"type in the first four numbers press enter after each one"<< endl;
cin>> na;
cin>> nb;
cin>> nc;
cin>> nd;
cout<<"now enter the number you want to get"<< endl;
cin>> ne;
k = 0;
while (k<=4){
k ++;
chance = perm (k, ea, eb, ec, ed);
if (chance==1){
ea = 1;
pa = na;
k=1;
};
if (chance==2){
eb = 1;
pb = na;
k=2;
};
if (chance==3){
ec = 1;
pc = na;
k=3;
};
if (chance==4){
ed = 1;
pd = na;
k=4;
};
j = 0;
while(j<=4){
j ++;
chance = perm (j, ea, eb, ec, ed);
if (chance==1){
ea = 1;
pa = nb;
j=1;
};
if (chance==2){
eb = 1;
pb = nb;
j=2;
};
if (chance==3){
ec = 1;
pc = nb;
j=3;
};
if (chance==4){
ed = 1;
pd = nb;
j=4;
};
l = 0;
while(l<=4){
l ++;
perm (l, ea, eb, ec, ed);
if (chance==1){
ea = 1;
pa = nc;
l=1;
};
if (chance==2){
eb = 1;
pb = nc;
l=2;
};
if (chance==3){
ec = 1;
pc = nc;
l=3;
};
if (chance==4){
ed = 1;
pd = nc;
l=4;
};
m = 0;
while (m<=1){
m++;
if (ea=0){
pa=nd;
ea=1;
}
else if(eb=0){
pb=nd;
eb=1;
}
else if (ec=0){
pc=nd;
ec=1;
}
else if (ed=0){
pd=nd;
ed=1;
};
computation (pa, pb, pc, pd, ne);
};
};
};
};
cout <<"thanks"<<endl;
cin.get();
};
int computation (float sa, float sb, float sc, float sd, float se){
float c = 0;
int thinga = 0;
while (thinga <= 8){
thinga ++;
c = comp (sa, sb, thinga);
float stepa = c;
int thingb = 0;
while(thingb <= 8){
thingb ++;
c = comp (c, sc, thingb);
float stepb = c;
int thingc = 0;
while (thingc <= 8){
c ++;
c = comp (c, sd, thingc);
float stepc = c;
if(c == se){
cout << sa<< "?"<< sb<< "="<< stepa <<endl;
cout << stepa<< "?"<< sc<< "="<< stepb <<endl;
cout << stepb<< "?"<< sd<< "="<< stepc <<endl;
cout << " " <<endl;
};
};
};
};
cout <<"theend"<<endl;
return 0;
};
float comp (float a, float b, int t){
float c;
if(t==1){
c = a + b;
};
if(t==2){
c = a - b;
};
if(t==3){
c = a * b;
};
if(t==4){
c = a / b;
};
if(t==5){
c = pow(a, b);
};
if(t==6){
c = b - a;
};
if(t==7){
c = b / a;
};
if(t==8){
c = pow(b, a);
};
return c;
};
int perm (int varo, int ea, int eb, int ec, int ed){
int chance;
if (varo==1){
if (ea==1){
varo++;
};
if (ea==0){
chance=1;
};
};
if (varo==2){
if (eb==1){
varo++;
};
if (eb==0){
chance=2;
};
};
if (varo==3){
if (ec==1){
varo++;
};
if (ec==0){
chance=3;
};
};
if (varo==4){
if (ed==1){
varo++;
};
if (ed==0){
chance=4;
};
};
return chance;
};
if someone could give me a nudge it the right diretion that would be great. thank you very much
-
Code:
$ g++ -W -Wall foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:111: warning: suggest parentheses around assignment used as truth value
foo.cpp:115: warning: suggest parentheses around assignment used as truth value
foo.cpp:119: warning: suggest parentheses around assignment used as truth value
foo.cpp:123: warning: suggest parentheses around assignment used as truth value
The first one being if (ea=0) which I'm guessing should be if (ea==0)
-
i hate it when i make stupid mistakes like that :eek: . my compiler did not pick up on that,is there any way i could make it do that?sadly though my program still dose not work :( :( :(here is the fixed code:
Code:
#include <iostream>
#include <math.h>
using namespace std;
float comp (float a, float b, int t);
int perm (int varo, int ea, int eb, int ec, int ed);
int computation (float sa, float sb, float sc, float sd, float se);
int main(){
float na;
float nb;
float nc;
float nd;
float ne;
int k;
int j;
int l;
int m;
int chance;
int ea = 0;
int eb = 0;
int ec = 0;
int ed = 0;
float pa = 0;
float pb = 0;
float pc = 0;
float pd = 0;
cout<<"type in the first four numbers press enter after each one"<< endl;
cin>> na;
cin>> nb;
cin>> nc;
cin>> nd;
cout<<"now enter the number you want to get"<< endl;
cin>> ne;
k = 0;
while (k<=4){
k ++;
chance = perm (k, ea, eb, ec, ed);
if (chance==1){
ea = 1;
pa = na;
k=1;
};
if (chance==2){
eb = 1;
pb = na;
k=2;
};
if (chance==3){
ec = 1;
pc = na;
k=3;
};
if (chance==4){
ed = 1;
pd = na;
k=4;
};
j = 0;
while(j<=4){
j ++;
chance = perm (j, ea, eb, ec, ed);
if (chance==1){
ea = 1;
pa = nb;
j=1;
};
if (chance==2){
eb = 1;
pb = nb;
j=2;
};
if (chance==3){
ec = 1;
pc = nb;
j=3;
};
if (chance==4){
ed = 1;
pd = nb;
j=4;
};
l = 0;
while(l<=4){
l ++;
perm (l, ea, eb, ec, ed);
if (chance==1){
ea = 1;
pa = nc;
l=1;
};
if (chance==2){
eb = 1;
pb = nc;
l=2;
};
if (chance==3){
ec = 1;
pc = nc;
l=3;
};
if (chance==4){
ed = 1;
pd = nc;
l=4;
};
m = 0;
while (m<=1){
m++;
if (ea=0){
pa=nd;
ea=1;
}
else if(eb==0){
pb=nd;
eb=1;
}
else if (ec==0){
pc=nd;
ec=1;
}
else if (ed==0){
pd=nd;
ed=1;
};
computation (pa, pb, pc, pd, ne);
};
};
};
};
cout <<"thanks"<<endl;
cin.get();
};
int computation (float sa, float sb, float sc, float sd, float se){
float c = 0;
int thinga = 0;
while (thinga <= 8){
thinga ++;
c = comp (sa, sb, thinga);
float stepa = c;
int thingb = 0;
while(thingb <= 8){
thingb ++;
c = comp (c, sc, thingb);
float stepb = c;
int thingc = 0;
while (thingc <= 8){
c ++;
c = comp (c, sd, thingc);
float stepc = c;
if(c == se){
cout << sa<< "?"<< sb<< "="<< stepa <<endl;
cout << stepa<< "?"<< sc<< "="<< stepb <<endl;
cout << stepb<< "?"<< sd<< "="<< stepc <<endl;
cout << " " <<endl;
};
};
};
};
cout <<"theend"<<endl;
return 0;
};
float comp (float a, float b, int t){
float c;
if(t==1){
c = a + b;
};
if(t==2){
c = a - b;
};
if(t==3){
c = a * b;
};
if(t==4){
c = a / b;
};
if(t==5){
c = pow(a, b);
};
if(t==6){
c = b - a;
};
if(t==7){
c = b / a;
};
if(t==8){
c = pow(b, a);
};
return c;
};
int perm (int varo, int ea, int eb, int ec, int ed){
int chance;
if (varo==1){
if (ea==1){
varo++;
};
if (ea==0){
chance=1;
};
};
if (varo==2){
if (eb==1){
varo++;
};
if (eb==0){
chance=2;
};
};
if (varo==3){
if (ec==1){
varo++;
};
if (ec==0){
chance=3;
};
};
if (varo==4){
if (ed==1){
varo++;
};
if (ed==0){
chance=4;
};
};
return chance;
};
thanks agian
-
you forgot to type "std::" before cout and cin. ( I dont know if its nessessary, but you could try it anyway)... I try...
-
-
You should use a debugger to find out what is going wrong. If you don't have one you can put a lot of cout statements to debug things and figure out what is going wrong.
Are you missing a thingc++ in the code? The first time thingc is used, it is set to 0, then passed to a function (comp) that is expecting a number between 1 and 8.
Sintu, it is not necessary, there is a global using directive in the code already.
-
>my compiler did not pick up on that,is there any way i could make it do that?
If there was, I wouldn't do it. The compiler allows it for a reason.
The if statement evaluates a condition. If the condition is true, it executes (not sure if that is technically the correct term) the block of code within the {brackets}. If not, it skips over the bracketed code. The variable = 4 part of
Code:
if (variable = 4) {blah blah blah}
is a condition and if the assignment of the value 4 to variable occurs, the condition is true, and the program will therefore execute the corresponding code block. It's just a part of how C++ works, not some flaw with the compiler. You just have to remember that = is strictly an assignment operator, and == is the comparison operator.
-
Even though it's legal, it is still a good idea to have your compiler identify those situations and output a warning. Salem showed how to do it with gcc (-W -Wall), and if you use an IDE like VC++ or Dev-Cpp there is probably a way to turn the warning level up in the project settings to get more warnings like that to help you.
-
So it would just warn you when it saw that type of condition in the if statement, but still compile? Neat. I was thinking more along the lines of giving an error and not compiling at all. Thanks for the clarification.
-
There are also options to treat warnings as errors.