Can't figure out what is wrong with my input

Can anyone try to tell me what is wrong with my input thus leading to a bad output ?
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
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct car
{
       char brand[41];
       char colour[21];
       float rent;
       char code[9];
};
void InputCarInfo(car p)
{
     char date[3];
     cout<<"Input the brand of the car: ";
     cin.getline(p.brand , 40 , ' ');
     cin.ignore(3);
     cout<<"Input the colour of the car: ";
     cin>>p.colour;
     cout<<"Input the rental price: ";
     cin>>p.rent;
     cout<<"Input the type of the car as they are: \n";
     cout<<"1-Small, 2-Compact, 3-Medium, 4-Van, 5-Jeep \n";
     cin>>date;
     p.code[0]=date[0];
     cout<<"Input the date the rent is due: \n";
     cout<<"Day:";
     cin>>date;
     p.code[1]=date[0];
     p.code[2]=date[1];
     cout<<"Month:";
     cin>>date;
     p.code[3]=date[0];
     p.code[4]=date[1];
     cout<<"Year:";
     cin>>date;
     p.code[5]=date[0];
     p.code[6]=date[1];
}
//void SortBrands(car array[], int size)
//{
     //car p;        //can't figure out how to initiallize p correctly
     //for(int i=0;i<size;i++)
     //for(int j=0;j<size-i;j++)
     //if(strcmp(array[j].brand,array[j+1].brand)==1)
     //{
                                                   //strcpy(p,array[j]);
                                                   //strcpy(array[j],array[j+1]);
                                                   //strcpy(array[j+1],p);
     //}
//}
void OutputCars(car p)
{
     cout<<"Brand: ";
     cout<<p.brand;
     cout<<endl;
     cout<<"Type(class): ";
     switch(p.code[0])
     {
                      case '1':cout<<"Small";break;
                      case '2':cout<<"Compact";break;
                      case '3':cout<<"Medium";break;
                      case '4':cout<<"Van";break;
                      case '5':cout<<"Jeep";break;
     }
     cout<<endl;
     cout<<"Colour: ";
     cout<<p.colour;
     cout<<endl;
     cout<<"Rental price: "<<p.rent;
     cout<<endl;
     cout<<"The date the rent is due is : ";
     cout<<p.code[1]<<p.code[2]<<"."<<p.code[3]<<p.code[4]<<"."<<p.code[5]<<p.code[6]<<".";
     cout<<endl;
}
int main()
{
    car dealership[200];
    int numberCars;
    cout<<"Input the number of cars in the dealership: ";
    cin>>numberCars;
    for(int i=0;i<numberCars;i++)
    InputCarInfo(dealership[i]);
    //for(int i=0;i<numberCars;i++)
    //for(int j=0;j<numberCars-i;j++)
    //SortBrands(dealership, numberCars);
    cout<<"_______________________________________"<<endl;
    for(int i=0;i<numberCars;i++)
    OutputCars(dealership[i]);
    cout<<"All of the cars in the dealership are: "<<numberCars<<endl;
    system("pause");
    return 0;
}
After playing with your code a little bit I found that adding cin.ignore(1); after each non-char input worked for me. I also changed cin.getline(p.brand , 40 , ' '); to cin.getline(p.brand , 40);. I think it is caused by the '\n' left in the buffer after number extraction. This is what I ended with:
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
void InputCarInfo(car p)
{
	 cin.ignore(1);
     char date[3];
     cout<<"Input the brand of the car: " << flush;
     cin.getline(p.brand , 40);
     cout<<"Input the colour of the car: ";
	 cin.getline(p.colour , 20);
     cout<<"Input the rental price: ";
     cin>>p.rent;
	 cin.ignore(1);
     cout<<"Input the type of the car as they are: \n";
     cout<<"1-Small, 2-Compact, 3-Medium, 4-Van, 5-Jeep \n";
     cin>>date;
     p.code[0]=date[0];
     cout<<"Input the date the rent is due: \n";
     cout<<"Day:";
     cin>>date;
     p.code[1]=date[0];
     p.code[2]=date[1];
     cout<<"Month:";
     cin>>date;
     p.code[3]=date[0];
     p.code[4]=date[1];
     cout<<"Year:";
     cin>>date;
     p.code[5]=date[0];
     p.code[6]=date[1];
}


You will also need to make car p a reference by adding an & before p. This makes p into dealership[i].
Last edited on
Indeed, it is now working, all thanks to you of course for which I am deeply grateful, however, can you tell me how to make the SortBrands function work ? Remove the comments and you will be able to see what errors I get .. I think all I need to do is initialize the p with the correct type, but I just can't figure out what would that type be..
You are using strcmp and strcpy. These are in <cstring>. http://www.cplusplus.com/reference/cstring/
You may want to use memcpy instead of strcpy. to get use size use sizeof(car). But I haven't used cstring before so I might be wrong.
I need to use exactly strcpy but I can't figure out what to initialize p as
To initialize p you could use car p = {};, this will initialize it with 0s. But the problem with your program is you are using strcpy on cars while it can only accept char*s
http://www.cplusplus.com/reference/cstring/strcpy/
I need to use exactly strcpy ...

As BlatantlyX has said, strcpy works with C (null-terminated) strings; not "cars"!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void SortBrands(car array[], int size)
{
    car p = {0}; // zero init -- {0} is popular as it works in C as well as C++
    for(int i=0;i<size;i++)
    { // I find brackets make code easier for me to read
        for(int j=0;j<size-i;j++)
        {
            if(strcmp(array[j].brand,array[j+1].brand)==1)
            {
                // memcpy works as it copies the car struct as a block
                memcpy((char*)&p,(char*)&array[j],sizeof(car));
                memcpy((char*)&array[j],(char*)&array[j+1],sizeof(car));
                memcpy((char*)&array[j+1],(char*)&p,sizeof(car));
            }
        }
    }
}


(Don't think your sort function is quite right??)

Andy

PS You should also rethink the way you're getting input. For example, if a careless user entered "1-Small" rather than just the 1 they'd overrun your buffer causing problems. And the way you're handling dates is rather unsafe, too. (It isn't that hard to accept a whole date string and parse it?)
Last edited on
Topic archived. No new replies allowed.