Demonstration in a programming course, a program will be created in which the then
Behavior of variables and associated pointer variables using functions demonstrated
should be. In the program are therefore two variable (eg a, b) of a selected type to
declare and initialize appropriate, and 2 pa pointer variable to that type (for example, and
pb), which refer initially to the two variables.
Then the following functions are to be compiled, the variables and pointer variables
To pass parameters (no global variables!) Are. Try loading the appropriate
Technology transfer (by value / reference) firmly.
1) 2 functions with the (same) name inputValue to enter the values of the two
Variables via keyboard. Which has a function as a parameter thereby references that
the other has two pointer variables as parameters.
2) 2 functions with the (same) name output value for displaying the values of the two
Variable on-screen prompts. Which has a function as a parameter while the variables themselves,
the other has two pointer variables as parameters.
Swap 3) The functions with names which swaps the values of the variables.
Additional voluntary task
4) A swap functions with (the same) names that the values of the pointer variables
interchanged.
My Programm:
I wanted to run the programm but t is showing some errors:
unused variable a , unused variable b , main must return int
,
unused variable pb , pa
Can somebody tell me what I am doing wrong.
I know that you people dont want to help if someone does not try but I tried this time.
you declared two variables 'a' and 'b' that are not used in the function.
How should I solve this problem?
I am sorry but I dont know.
Otherwise I wouldn `t ask.
In this function
1
2
3
4
5
6
7
8
9
void inputValue(int *pa, int *pb)
{
int a, b;
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cout<<" A : ";
cin>>a;
cout<<endl<<" B : ";
cin>>b;
}
The first thing to realise is that compilers usually tell you what line your error is on. You should use that to help you identify where your errors are.
At line 4, you have the line:
int main(){
You don't close the brace, and you immediately then start another function. This is the cause of your compiler error.
Your function inputValue takes two arguments, pa and pb, but you don't use them anywhere in that function. This is the cause of your warnings.
void outputVlaue(int *pa, int *pb) // outputValue maybe?
{
int a=0, b=0; // Not used, you can remove this line
cout<<" Zeiger A ist = "<<pa<<endl;
cout<<" Zeiger A ist = "<<pb<<endl; // Logic (human) error, "Zeiger A" should be "Zeiger B" ?
}
Some lines down...
1 2
swap(a,b); // swap isn't taking integers, it takes pointers.
// use swap(pa,pb) instead.
It's all right, just give it a try. Remember that your compiler chooses to use the pass-by-value/pass-by-reference with inputValue and outputValue, so you can remove the pointer-ones.
(You can remove from line 16 to 24, and from line 32 to line 38. Also you can change your 'swap' code to use reference and be more consistent about using your code)
#include <iostream>
usingnamespace std;
void inputValue(int &a, int &b)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cin>>a;
cin>>b;
}
void inputValue(int *pa, int *pb)
{
cout<<"Geben Sie Ihre Zahlen an :"<<endl;
cout<<" A : ";
cin>>*pa;
cout<<endl<<" B : ";
cin>>*pb;
}
void outputValue(int a, int b)
{
cout<<" A ist = "<<a<<endl;
cout<<" B ist = "<<b<<endl;
}
void outputVlaue(int *pa, int *pb)
{
cout<<" Zeiger A ist = "<<pa<<endl;
cout<<" Zeiger B ist = "<<pb<<endl;
}
void swap(int *pa, int *pb)
{
int tmp;
tmp=*pa;
*pa=*pb;
*pb=tmp;
}
int main()
{
int a, b;
int *pa=&a;
int *pb=&b;
inputValue(a,b);
swap(pa,pb);
outputValue(a,b);
//return 0;
system("PAUSE");
//return EXIT_SUCCESS;
}
But how can i create with switch case a menu?
Because in the task is also written:
The main program then should contain a menu, in which the for example lecturer repeated one of the functions
Can select and activate functions or exit the program