Programming C++

Pages: 12
Hello people I have problems with this task:

I have almost solved the task :

Here the task:

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.

Please help me so that the program works.
Last edited on
In this function

1
2
3
4
5
6
7
void outputVlaue(int *pa, int *pb)
{
    int a=0, b=0;

    cout<<" Zeiger A ist = "<<pa<<endl;
    cout<<" Zeiger A ist = "<<pa<<endl;
}


you declared two variables 'a' and 'b' that are not used in the function.

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;
}


parameters 'pa' and 'pb' are not used.

Function main shall be declared as

int main()
Main needs to be declared like this:
1
2
3
4
5
6
int main()
{
    // stuff

    return 0;
}


Change it to that and see if you still get any errors.
My programm is now lookin so , but its still showing error.

{ before token .

What should I do?


ANd I wanted to ask you if i have declared the functions right or have I done something wrong?
Last edited on
You did not change anything as it was pointed to.
I did this: You told me:

Function main shall be declared as

int main()

I putted the function into int main.

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;
}


parameters 'pa' and 'pb' are not used.

hOW SHOULD i CORRECT THIS ß
Hey all experts . Can please someone Tell me how I can correct this Programm.
The compiler tells you that you don't use pa and pb. So I don't know why you don't use them:
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*pa;
    cout<<endl<<" B : ";
    cin>>b*pb;
}


You have outputVlaue where you just outpu pa. I won't show you how to output pb
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.


Is the Programm now alright?
Last edited on
Um... you have two main functions. Why?
Oh yes you are right. Oh which Main should I eliminate ?

Sorry now I am a little confused.
delete the int main and rename the void main to int main


Have I know solved the task correctly or are there some mistakes ?
Last edited on
Is the task now rightly solved?
1
2
3
4
5
6
7
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. 
I think without your help I wouldn`t have don it right.




Is the task now solved rightly?

Or are there some more mistakes?
Last edited on
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)
Last edited on
Oh that `s good thank you all.

But can you tell me how I can test the code?

When I run the programm and type for example 2 , 3 .

It changes and says Zeiger A ist =3 , B ist = 2

Is that all?

Im asking only for information.
Hello people my code is so and its working .

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
#include <iostream>
using namespace 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


Can somebody tell me how I can do this?
Pages: 12