Vector Function (Continued)

So back again, essentially the program is functioning so long as the user inputs the proper variables. However, I would like to program to break if any values are put improper. This appears to be the case in two parts of my program. When I enter the dimensions for my Vectors, If i were the user, and I entered '1 2' for "What are the dimensions of Vector 1?". The program stops functioning, but will print "What are the dimensions of Vector 2?" followed by "The Dimensions of your vector do not match." Which obviously this does not look aesthetically good. Also, assuming I put in the same dimensions for both vectors, if i were to input 3 dimensions for vector 1 and 2, then I entered the values for vector 1 to be (1, 2, 3, 4) and then vector 2 i entered values (5,6), the program will carry own and basically output "Values for vector 1 are '1,2,3'" and "values for vector 2 are '4,5,6'". So essentially, I want the program to stop if I've either exceeded the total values allowed by the array in my program or if I didn't put enough values that match the array length. Thanks for any help! :)

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

using namespace std;

bool vect(int n, int m)
{
    if(n==m)
        return 1;
    else
        return 0;
}

void printvect (int *vec1, int *vec2,int*a,int *b, int *c)
{
cout<<"The values for Vector 1 are (";
    for(int i=0;i<*a-1;i++)
    {
        cout<<vec1[i]<<" , ";
    }
    cout<<vec1[*a-1]<<")"<<endl<<endl;
    cout<<"\nThe values for Vector 2 are (";
    for(int i=0;i<*b-1;i++)
    {
        cout<<vec2[i]<<" , ";
    }
    cout<<vec2[*b-1]<<")"<<endl<<endl;

    cout<<"The inner product of your Vectors is (";
double vectotal=0;
    for(int i=0;i<*c;i++)
    {
    vectotal=(vec1[i]*vec2[i])+vectotal;
    }

    cout<<vectotal<<")"<<endl;;

}

int main()
{
    int n;
    int m;
    cout<<"What are the dimensions for Vector 1?"<<endl;
    cin>>n;
    cout<<"What are the dimensions for Vector 2?"<<endl;
    cin>>m;
    if
        (!vect(n,m))
        {cout<<"The dimensions of your vector do not match."<<endl;
        return 0;}
    else if (vect(n,m))
    {
        int vec1[n];
        int vec2[m];
        int k = (m+n)/2;

        cout<<"Enter the values for Vector 1."<<endl;
        for(int i=0;i<=n-1;i++)
          {cin>>vec1[i];
          }
        cout<<"Enter the values for Vector 2."<<endl;
        for(int j=0;j<=m-1;j++)
        {
            cin>>vec2[j];
        }
        printvect(vec1,vec2,&n,&m, &k);


     return 0;
    }



        return 0;
}
first, your issue is about input verification, the vector part is just incidental.
second, question/answer interface is bothersome.

To read a line you may use getline() http://www.cplusplus.com/reference/string/string/getline/
Then may convert it with a stringstream
1
2
3
4
5
6
7
8
std::string line;
while(std::getline(std::cin, line)){
	std::istringstream input(line);
	int n;
	while(input >> n){
		//process each number
	}
}
Topic archived. No new replies allowed.