Need help with hexadecimal addition program

it's something small
i can only press lower case 'q' to stop the entry of each hexadecimal entry

the code works but i want it so that i press enter after each hexadecimal entry

this is my code so far:

--------------------------------------------------------------------

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

#include <iostream>
using namespace std;

void intro();
void input();

void input(char[]);
void output(char[]);
void hex_sum(char[], char[], char[]);

int main()
{
    char answer;

    intro();

    do
    {
        input();

        cout << endl << endl << "Do you wish to convert another hexadecimal number? (y/n) \n";
        cin >> answer;

    } while(answer == 'y' || answer == 'Y');

    cout << "Good-bye! \n";

    return 0;
}

void intro()
{
    cout << "Enter a hexadecimal number of 10 or fewer digits \n"
         << "0-9 and A-F (in caps) \n"
         << "Press lower case q to stop the entry \n" << endl;
}

void input()
{
    char number_1[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
    char number_2[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
    char sum[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
    char temp[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'}; //initializing all the variables

    cout << "Enter the first hexadecimal number \n";
    input(number_1);
    cout << endl;

    cout << "Enter the second hexadecimal number \n";
    input(number_2);
    cout << endl;

    hex_sum(number_1, number_2, sum);;

    cout << "The addition of the two hexadecimal numbers is \n";
    output(sum);
}

void input(char number[])
{
    int i = 0;
    char c = '0', tmp;

    while(c != 'q') //this is my problem right here!
    {
        cin >> c;
        number[i] = c;
        i++;
    }

    number[--i] = '0';

    for(int k = 0; k < i/2; k++)
    {
        tmp = number[k];
        number[k] = number[i - 1 - k];
        number[i - 1 - k] = tmp;
    }
}

void output(char number[])
{
    for(int i = 9; i >= 0; i--)
    {
        cout << number[i];
    }
}

void hex_sum(char number_1[], char number_2[], char sum[])
{
    int x, y, s, carry = 0, next_carry = 0;

    for(int i = 0; i < 10; i++)
    {
        if('0' <= number_1[i] && number_1[i] < '0' + 10)
            x = number_1[i] - '0';

        else
            x = number_1[i] - 'A' + 10;

        if('0' <= number_2[i] && number_2[i] < '0' + 10)
            y = number_2[i] - '0';

        else
            y = number_2[i] - 'A' + 10;


        carry = next_carry;

        s = (x + y + carry) % 16;
        next_carry = (x + y + carry) / 16; //converts the number back to hex

        if(0 <= s && s < 10)
            sum[i] = char('0' + s);

        else if(10 <= s && s < 16)
            sum[i] = char('A' + s - 10);

        else
            cout << "Invalid input \n";
    }

    if(1 == carry && 1 == next_carry)
        cout << "Overflow! \a \n";
}


1
2
3
4
5
6
 while(c != 'q')
    {
        cin >> c;
        number[i] = c;
        i++;
    }

---------------------------------------------------------------------
thanks in advance
Last edited on
change 'q' in '\n', this is the caracter of 'new line':
1
2
3
4
5
6
while(c != '\n') // <--- changed 'q' in '\n'
    {
        cin >> c;
        number[i] = c;
        i++;
    }
i tried it but nothing happens when i press enter after each hexadecimal entry
you can use cin.get() to store the entire input into a temporary string, then copy each value into the vector of char:
1
2
3
4
5
6
7
8
9
void input(char number[])
{
    char strInput[11]  //11 because 10 elements + 1 for the '\0' character added by getline()
    int i = 0;
    char c = '0', tmp;
    cin.get(strInput,11);  //getline()extract 10 char from stdin
    cin.ignore(1000); //clear the buffer of stdin by discarding 1000 remaining character or until '\n' is found , for a future input
    for(int i=0;i<9;++i) number[i]=strInput[i];
    number[9]='0';
something like this would save some space:
1
2
3
4
5
6
7
8
9
int hex_1,hex_2;
cout<<"\n\nadd two numbers in hexadecimal format \n";
cout<<"enter first number \n";
cout<<hex;cin>>hex;
cin>>hex_1;
cout<<"enter second number \n";
cin>>hex_2;
cout<<"\nthe sum  of these two numbers is "<<hex_1 + hex_2;
    
Topic archived. No new replies allowed.