my program does nothing

This is a discussion on my program does nothing within the C++ Programming forums, part of the General Programming Boards category; i wrote a program that plays this game we do in math class. you are given five numbers, say 5 ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Talking 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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    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)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    i hate it when i make stupid mistakes like that . 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

  4. #4
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    you forgot to type "std::" before cout and cin. ( I dont know if its nessessary, but you could try it anyway)... I try...
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    it still dosent work.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  7. #7
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    >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.
    There is a difference between tedious and difficult.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  9. #9
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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 is a difference between tedious and difficult.

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    There are also options to treat warnings as errors.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21