being forced to use only one variable in function which needs two variables

Here's the error I get in Visual C++:
1>------ Build started: Project: testing stuff, Configuration: Debug Win32 ------
1> testing stuff.cpp
1>c:\users\hp\documents\visual studio 2010\projects\testing stuff\testing stuff\testing stuff.cpp(33): error C2065: 'numberOfColumns' : undeclared identifier
1>c:\users\hp\documents\visual studio 2010\projects\testing stuff\testing stuff\testing stuff.cpp(33): error C2078: too many initializers
1>c:\users\hp\documents\visual studio 2010\projects\testing stuff\testing stuff\testing stuff.cpp(39): error C2065: 'numberOfColumns' : undeclared identifier
1>c:\users\hp\documents\visual studio 2010\projects\testing stuff\testing stuff\testing stuff.cpp(39): error C2078: too many initializers
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have no idea how to solve this, since choice 1 and choice 2 both depend on two variables, but Visual C++ wants me to only let choice 1 and choice 2 depend on one variable each!

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
#include "stdafx.h"
#include <iostream>

using namespace std;

void chooseValues(int choice) //choose values of y and numberOfColumns
{
	cout << "Please enter the constant number: ";
	
	if(choice == 1)
		{
			int y;
			cin >> y;
			int choice1(y);
		}
		else if(choice == 2)
		{
			int x;
			cin >> x;
			int choice2(x);
		}
		else
		{
			cout << "Sorry, you didn't choose 1 or 2.\n";
		}
		
}

void choice1(int y, int numberOfColumns)
{
	cout << "\nHere are the values of x % " << y << ": \n";
	
	for(int iii = 0; iii < ( (numberOfColumns + 1) * y); iii++)
	{
	cout << iii << " % " << y << " = " << iii % y << ".";
		if( iii % y == y - 1 )
			cout << "\n";
		else
			cout << "\t";
	}
}

void choice2(int x, int numberOfColumns)
{ 
	cout << "\nHere are the values of " << x << " % y: \n";
	
	for(int iii = 0; iii < ( (numberOfColumns + 1) * x); iii++)
	{
	cout << iii << " % " << x << " = " << iii % x << ".";
		if( iii % x == x - 1 )
			cout << "\n";
		else
			cout << "\t";
	}
}

int main()
{
	cout << "This program outputs values of x % y.\n";
	cout << "Enter '1' if you want to see values of x % constant number.\n";
	cout << "Enter '2' if you want to see values of constant number % y.\n";
	int choice;
	cin >> choice;
	cout << "Please enter how many columns you want: ";
	int numberOfColumns;
	cin >> numberOfColumns;
		
	int chooseValues(choice);
				
	cout << "\n Have a great day! \n";
	return 0;
}


 


You're only passing one argument into choice1() and choice2(), which require 2 arguments.
There are a few problems here. For example, line 68, this is a function declaration, it tells the compiler the name of the function, and what type of parameters it takes.
int chooseValues(choice);
But what you really want is an actual function call, more like this:
chooseValues(choice);

The same problem is at lines 14 and 20, you have a declaration instead of calling the function.

Another issue is that you do need to declare function prototypes, in order that the compiler can know about the existence of a function when you call it, even though it will be defined further down in the code.

As well as those problems, there is the one you mentioned, you need to pass the right number of parameters to each function. The end result is something like this, it should compile but I have not tested it.
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
#include <iostream>

using namespace std;

// declare the function prototypes
void choice1(int y, int numberOfColumns);
void choice2(int x, int numberOfColumns);

// define the functions
void chooseValues(int choice, int numberOfColumns) //choose values of y and numberOfColumns
{
    cout << "Please enter the constant number: ";

    if(choice == 1)
        {
            int y;
            cin >> y;
            choice1(y, numberOfColumns);
        }
        else if(choice == 2)
        {
            int x;
            cin >> x;
            choice2(x, numberOfColumns);
        }
        else
        {
            cout << "Sorry, you didn't choose 1 or 2.\n";
        }

}

void choice1(int y, int numberOfColumns)
{
    cout << "\nHere are the values of x % " << y << ": \n";

    for(int iii = 0; iii < ( (numberOfColumns + 1) * y); iii++)
    {
    cout << iii << " % " << y << " = " << iii % y << ".";
        if( iii % y == y - 1 )
            cout << "\n";
        else
            cout << "\t";
    }
}

void choice2(int x, int numberOfColumns)
{
    cout << "\nHere are the values of " << x << " % y: \n";

    for(int iii = 0; iii < ( (numberOfColumns + 1) * x); iii++)
    {
    cout << iii << " % " << x << " = " << iii % x << ".";
        if( iii % x == x - 1 )
            cout << "\n";
        else
            cout << "\t";
    }
}

int main()
{
    cout << "This program outputs values of x % y.\n";
    cout << "Enter '1' if you want to see values of x % constant number.\n";
    cout << "Enter '2' if you want to see values of constant number % y.\n";
    int choice;
    cin >> choice;
    cout << "Please enter how many columns you want: ";
    int numberOfColumns;
    cin >> numberOfColumns;

    chooseValues(choice, numberOfColumns);

    cout << "\n Have a great day! \n";
    return 0;
}

Topic archived. No new replies allowed.