cool math multiple program funtion problem

this cool program i thought of this program determines for a pair of integers whether the second integer is a multiple of the first.but it is not compiling it says that multiple cant be used as a function when i want to and something about a globe scopes i don't see whats wrong with the multiple?

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
//p.s. i have all theses includes because i think there so kick a$$
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;


void multiple(int,int);


int main()
{ 
        [code]int FALSE;
        [code]int TURE;
        int multiple;   
        int num1;
        int num2;
	    int y;
	    int x;
	
	cout<<"enter two numbers to see if the first number is multiple"<<endl;
	     cout<<"by the first."<<endl;
	     cin >>num1;
	     cin >>num2;
	     
        multiple(num1,num2);
	
	if(multiple(x,y))
    {
       cout<<"TRUE it is a multiple"<<endl;
    }  
       else cout<<"FALSE not a multiple"<<endl;
       
       
       
       system("pause");
       return 0;


}

    
     void multiple(int num1,int num2);

 {    
   
      a = num1;
      b = num2;
   
    if (multiple(a,b)) 
     {
        x = b / a && a * x = b;
        x = TURE;
     }
       
 }

line 45: void multiple(int num1,int num2);

The semicolon at the end terminates the function before it even starts... You have to remove it... Also you use int the function variables named 'a' and 'b' that you never declared...
Last what do you want to do by saying: x = b / a && a * x = b; ??? It trully doesn't make any sence...
The multiple function is all wrong and without any meaning... Rethink what you are trying to do...
Last edited on
make the function return true or false
Also use % to find out it is divisble or not
The function is void. You can not have it returning a value...
Some more about your code:
You can not have a variable and a function with the same name... That's why you have the error about the usage of multiple...
[edit] At least not in the same scope. The local one will override the other.
if(multiple(x,y))
multiple is a void function, it can not be used as a statement... Also the 'x' and 'y' have no values, so the call to the function will have no meaning...
To have variables in global scope you must have them before the main().

You should read a little more about the functions...

And as anilpanicker sayed use the modulo ( % ) operator to find if it is a multiple...
Last edited on
multiple() is defined as returning void, so you can't use it in an expression.

I think what you would like to do is
1
2
3
4
5
bool multiple( int n, int divisor )
  {
  // return true or false depending on whether 'n' is a multiple of 'divisor'
  // (that is, whether or not 'divisor' evenly divides 'n')
  }

Remember, you must declare a variable before you use it. Since num1 and num2 are the same as a and b, why create extra variables?

If you want to make the routine recursive, you must provide a way out. Currently it does not. An excellent example is the GCD algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Euclid's method
int euclid_gcd( int a, int b )
  {
  if (b == 0)
    return a;
  else
    return euclid_gcd( b, a % b );
  }

// Dijkstra's method
int dijkstra_gcd( int a, int b )
  {
  if (a == b)
    return a;
  else if (a > b)
    return dijkstra_gcd( a-b, b );
  else
    return dijkstra_gcd( a, b-a );
  }

In both cases, there are at least two branches of execution: at least one without a recursive call and others with a recursive call. For Euclid, we quit when the divisor becomes zero (there are no more divisors). Dijkstra does basically the same thing, except instead of using modulus he uses straight subtraction. When the two sides equal then they must be the common divisor.

The global scope complaint is because you are trying to use the variable x that exists in the function main(), but from the function multiple(). You can't do that, so the compiler doesn't even look for x in main() and complains that it does not exist as a global variable.

Hope this helps.

[edit] Woah, I'm a slowpoke...
Last edited on
Woah, I'm a slowpoke...

It doesn't matter... you give the sophisticated answers :D
since then i add a hole assortment of other problems this is really an experiment to make one big menu program with many math helper tools for young people but in my problem 3 i made my own function but im still getting errors (a funtion definition is not allowed here?)

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
void Menu();
int getN();
void def(int);
void problem1();
void problem2();
void problem3();
void problem4();
void problem5();
void problem6();
void problem7();
void multiple();    
    int main(int argv,char *argc[])
    {
	int inN;
        do{
         Menu();
         inN=getN();
         switch(inN){
          case 1:    problem1();break;
          case 2:    problem2();break;
          case 3:    problem3();break;
          case 4:    problem4();break;
          case 5:    problem5();break;
          case 6:    problem6();break;
          case 7:    problem7();break;
          default:   def(inN);}
        }while(inN<8);
        return 1;
    }
    void Menu()
    {
           cout<<"Type 1 for problem 1"<<endl;
           cout<<"Type 2 for problem 2"<<endl;
           cout<<"Type 3 for problem 3"<<endl;
           cout<<"Type 4 for problem 4"<<endl;
           cout<<"Type 5 for problem 5"<<endl;
           cout<<"Type 6 for problem 6"<<endl;
           cout<<"Type 7 for problem 6"<<endl;
           cout<<"Type 8 to exit \n"<<endl;
    }
    int getN()
    {
           int inN;
		   cin>>inN;
           return inN;
    }
    void problem1()////////////////////////
    {
     {
  	  {
    void problem1 ();
     unsigned short n,s=0;
    cout<<"please enter a number ";
    scanf ("%d",&n);
    while (n!= 0)
    {
        s = (10 * s) + n % 10;
        n = n / 10;
    }
    printf ("The Reversed Number is %d",s);
    getch ();  
        system("CLS");	
        }
	 }		
    }
    void problem2()/////////////////////////
    {
    int hidenum,guess,stop = 0;
    char choice;
    srand(time(NULL));
    hidenum = rand() % 1000 + 1;
     while(hidenum != guess)
     {
          cout<<"guess a number from 1 to 1000: "<<endl;
          cin >>guess; 
    if(hidenum < guess) 
    {
           cout<<"The secret number is lower"<<endl;
           stop++;
    } 
    if(hidenum > guess)
    {
           cout<<"the secret number is higher"<<endl;
           stop++;
    }
    if(hidenum == guess)
    {
           cout<<"you did it you won!!!"<<endl;
           system("pause");
           cout<<"would you like to play agian y for yes n for no?"<<endl;
           cin >>choice; 
              if(choice == 'Y'|| choice == 'y')    
                  {
                      cout<<"yahh thats the spirit!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  problem2();
                  }
              if (choice == 'N'|| choice == 'n')    
                 {
                      cout<<"noooooo.. you lose!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  Menu();
                 }
     }
     if(stop > 9)
     {
           cout<<"you failed to find the number"<<endl;
           cout<<"would you like to play agian y for yes n for no?"<<endl;
           system("pause");
           cin >>choice;
              if(choice == 'Y'|| choice == 'y')    
                  {
                      cout<<"yahh thats the spirit!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  problem2();
                  }
              if (choice == 'N'|| choice == 'n')    
                  {
                      cout<<"noooooo.. you lose!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  Menu();
                  }  
     }
    }
    }
    void problem3()/////////////////////////
    {
   
   int multiple(int x, int y)
   {
     if(y%x == 0)
     return 1;
      else
     return 0;
   }
    }
    void problem4()////////////////////////
    {
	}
    void problem5()////////////////////////////
    {	
    }
    void problem6()
	{	   	   
   	}
    void problem7()
	{	   
	}
	void def(int inN)
    {
     
      {
           cout<<"You typed "<<inN<<" to exit the program"<<endl;
      }
    
    }
    void multiple()
    {

    int multiple(int,int);
    int a,b,ans;
       cout<<"Enter two numbers : ";
       cin>>a>>b;
       ans=multiple(a,b);
    if(ans==0)
       cout<<b<<" is a multiple of "<<a;
    else
       cout<<b<<" is not a multiple of "<<a;
    
    return Menu();
    } 



You have a function in a function...
1
2
3
4
5
6
7
8
9
10
11
void problem3()/////////////////////////
{

   int multiple(int x, int y)
   {
      if(y%x == 0)
         return 1;
      else
         return 0;
   }
}

You probably want to close the first function before starting the second one...

Also what are all those brackets? you open and close brackets without any reason..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void problem1()////////////////////////
{
   {
      {
         void problem1 ();
         unsigned short n,s=0;
         cout<<"please enter a number ";
         scanf ("%d",&n);
         while (n!= 0)
         {
            s = (10 * s) + n % 10;
            n = n / 10;
         }
         printf ("The Reversed Number is %d",s);
         getch ();  
         system("CLS");	
      }
   }		
}
I always type the closing brace immediately after the opening brace, then fill it in. This solves almost every one of the problems I might have with mis-matched braces.
1
2
3
if (foo)
  {_
| | Now to add the closing brace
V
1
2
3
if (foo)
  {
  }_
| | Great, now I can fill in the innards
V
1
2
3
4
if (foo)
  {
  cout << "fooey!\n";_  
  }

I think this is a very good habit to get into. Always write open and close braces at the same time.

Also, put some spaces or draw a line or something between functions. I tend to use a bunch of dashes. It helps me visually separate distinct pieces of code.
1
2
3
4
5
6
7
8
9
10

//----------------------------------------------------------------------------- 
// foo()
//   Does fooey, and terminates the program.
//
void foo()
  {
  cerr << "fooey!\n";
  exit( 1 );
  }


Hope this helps.
I too am in the habbit of typing the closing brace right after I type the opening brace. It really helps me, cause I don't have to keep track of how many braces I need.

The only problem I run into, with that however.. is If I need to add more code after a closing brace. e.g.

1
2
3
4
5
6
7
8
9
10
11
12
if ( ... )
{
  do (this);
  if ( ... )
  {
    do (that);
    if ( ... )
    {
      do (something, else);
    }
  }
}


Now I need to add do (nothing); in the first if, but not in the other two. which brace is that again?

After a while of coding, one brace looks like another.. it can get confusing. I have seen someone else do something that I am thinking of trying to adopt.

1
2
3
4
5
6
7
8
9
10
11
12
if ( ... ) // first if
{
  do (this);
  if ( ... ) // second if
  {
    do (that);
    if ( ... ) // third if
    {
      do (something, else);
    } // end third if
  } // end second if
} // end first if 


Of course the comments would have to be more descriptive than first if, and end first if, but you get the point.
That's a very common style. I've done it myself when the code is fairly complicated.

Some editors are smart enough to match braces for you.

I always indent two spaces under the head line, so I can just place the cursor at the brace to be matched, then scroll up or down to find its match. If I'm trying to fix mismatched braces, I'll count open/close braces as I go until I hit zero.

But that's me, because I like my old editor too much. Unfortunately it is too stupid to properly match curly braces...
Topic archived. No new replies allowed.