input value into 2 variables ?

#include <iostream>
using namespace std;

int main(void)
{
int n;
char a[100];
cin >> a;
return 0; }

this inputs what the user writes to the array a, but what if i want to input what the user writes into both array a and integer n without forcing the user to write twice, is this possible ?
Last edited on
First of all, please wrap your code in the code tags -- you'll find them under the Format: section.

It is possible, but dangerous: You will not be able to easily test for bad user input.

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

int main( void )
    {
    char* n = 0;

    char a[ 100 ];

    n = &a[ 0 ];

    cin >> a;

    return 0;

    }
        /*    main()    */

What do you want integer n to contain after input?
Show an example of input and what n and a should contain.
@koothkeeper: thanks but this inputs the value to two entities of type char*, isn't it possible to input the value in char* variable and int variable ?
@MiiNiPaa: for example a program receiving numbers from user in a while loop to calculate their largest one, at the same time if the user entered the word "end" the program breaks the loop and outputs the largest value, so i need to put what the user writes in both int and array variables
i figured out a way for the idea of the program !
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
#include <iostream>
using namespace std;

int main( void )
    {
	char max[100];
        char a[100];
	int nn=2;
	cout << "enter value number 1 : ";
        cin >> a;
	for (int i=0; i<100; i++)
	max[i]=a[i];
	while(1)
	{
	cout << "enter value number " << nn << " : ";
	cin >> a;
	if (a[0]=='e' && a[1]=='n' && a[2] == 'd')
	break;
	if (atoi(a)>atoi(max))
	{
        for (int j=0; j<100; j++)
	max[j]=a[j];}
	nn++;
	}
	cout << "largest value : " << max << endl;
        return 0;
    }

it works on my visual studio
Last edited on
more shortened way :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main( void )
    {
	int max, n=2;
        char a[100];
	cout << "enter value number 1 : ";
        cin >> a;
	max=atoi(a);
	while(1)
	{
	cout << "enter value number " << n << " : ";
	cin >> a;
	if (a[0]=='e' && a[1]=='n' && a[2] == 'd')
	break;
	if (atoi(a)>max)
	max=atoi(a);
	n++;
	}
	cout << "largest value : " << max << endl;
        return 0;
    }
Last edited on
Topic archived. No new replies allowed.