Vector question

Hello! (sorry if my english is bad I'm from Sweden)

I have a little problem. Look at this code:
1
2
3
4
5
int persnr[10];
    cin >> persnr[0] >> persnr[1] >> persnr[2] >> persnr[3] >> persnr[4] >> persnr[5] >> 
    persnr[6] >> persnr[7] >> persnr[8] >> persnr[9];
    cout << persnr[0] << persnr[1] << persnr[2] << persnr[3] << persnr[4] << persnr[5] << 
    persnr[6] << persnr[7] << persnr[8] << persnr[9];


It's part of a program I have done. When you type in the numbers you have to make a space (or enter) between every number example "8 9 0 1... " In order to make it work. What I want is that the user just can type "8901..." and its stores the same as when you type "8 9 0 1..". I can do this with a string but then it doesn't stores as a integer.

I hope you understand what I want to do. Here is the whole program if it's any help. So you can see what the program does:

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
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstdlib>
#include <climits>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;



int main()
{
    cout << "Skriv in ditt personnummer ååmmddxxxx: ";
    int persnr[10];
    cin >> persnr[0] >> persnr[1] >> persnr[2] >> persnr[3] >> persnr[4] >> persnr[5] >> 
    persnr[6] >> persnr[7] >> persnr[8] >> persnr[9];
    cout << persnr[0] << persnr[1] << persnr[2] << persnr[3] << persnr[4] << persnr[5] << 
    persnr[6] << persnr[7] << persnr[8] << persnr[9];
    
    int tal1 = persnr[0]*2;
    if (tal1==10)
    {
                 tal1=1;
    }
    if (tal1>10)
     {
         tal1=(tal1%10)+1;
     }
///////////////////////////////////////////////////////////    
    int tal2 = persnr[1]*1;
    if (tal2==10)
    {
                 tal2=1;
    }
    if (tal2>10)
     {
         tal2=(tal2%10)+1;
     }
///////////////////////////////////////////////////////////      
    int tal3 = persnr[2]*2;
    if (tal3==10)
    {
                 tal3=1;
    }
    if (tal3>10)
     {
         tal3=(tal3%10)+1;
     }
///////////////////////////////////////////////////////////  
    int tal4 = persnr[3]*1;
    if (tal4==10)
    {
                 tal4=1;
    }
    if (tal4>10)
     {
         tal4=(tal4%10)+1;
     }
///////////////////////////////////////////////////////////  
    int tal5 = persnr[4]*2;
    if (tal5==10)
    {
                 tal5=1;
    }
    if (tal5>10)
     {
         tal5=(tal5%10)+1;
     }
///////////////////////////////////////////////////////////  
    int tal6 = persnr[5]*1;
    if (tal6==10)
    {
                 tal6=1;
    }
    if (tal6>10)
     {
         tal6=(tal6%10)+1;
     }
///////////////////////////////////////////////////////////  
    int tal7 = persnr[6]*2;
    if (tal7==10)
    {
                 tal7=1;
    }
    if (tal7>10)
     {
         tal7=(tal7%10)+1;
     }
///////////////////////////////////////////////////////////  
    int tal8 = persnr[7]*1;
    if (tal8==10)
    {
                 tal8=1;
    }
    if (tal8>10)
     {
         tal8=(tal8%10)+1;
     }
/////////////////////////////////////////////////////////// 
    int tal9 = persnr[8]*2;
    if (tal9==10)
    {
                 tal9=1;
    }
    if (tal9>10)
     {
         tal9=(tal9%10)+1;
     }
/////////////////////////////////////////////////////////// 
    
    int sum = tal1+tal2+tal3+tal4+tal5+tal6+tal7+tal8+tal9;
    
    int sum2 = sum%10;
    int kontroll = 10-sum2;
    
    if(persnr[8]==1&&persnr[9]==kontroll||persnr[8]==3&&persnr[9]==kontroll||persnr[8]==5&&persnr[9]==kontroll||
    persnr[8]==7&&persnr[9]==kontroll||persnr[8]==9&&persnr[9]==kontroll)
    {
        cout << "\nDu är en man.\n";
        cout << "Din kontrollsiffra är: " << kontroll;
    }
    
   else if(persnr[8]==2&&persnr[9]==kontroll||persnr[8]==4&&persnr[9]==kontroll||persnr[8]==6&&persnr[9]==kontroll||
   persnr[8]==8&&persnr[9]==kontroll)
    {
        cout << "\nDu är en kvinna.\n";
        cout << "Din kontrollsiffra är: " << kontroll;
    }
    
    else
    {
        cout << "\nDu har angivit ogiltligt personnummer!";
    }
    
cout << endl;
cout << endl;
system("PAUSE");
return 0;
}
you can use atoi function call to convert string to integer.

Hope this helps !
atoi isn't an ideal solution. As on failure it'll return 0. But in a lot of cases 0 is a valid result that could be used.

As I posted a link that illustrates how to use a string-stream to accomplish the conversion. On failure this will throw an exception that can be handled accordingly.
atoi isn't an ideal solution. As on failure it'll return 0. But in a lot of cases 0 is a valid result that could be used


Excellent point.

Thanks for pointing that out.
Thanks for the link! But I can't get this to work with a vector. It would be nice if someone could write down the code. Because I'm getting crazy. I've tested everyting I can come up with but nothing wroks :(

I have not programmed for so long in C++ so that's why I may seem a little slow ;)

Very grateful for some more help!

EDIT: Never mind, I've fixed it now :D
Last edited on
Topic archived. No new replies allowed.