c++ read string

closed account (oj2wbRfi)
assume we have this struct and we need to take data from user , the type of the car and the car number, how can we read line nr 2 below, and how to read the code , I mean what does this code mean vector <string> type

1
2
3
4
5
6
7
8
9
10
11
12
13
 struct Car {
vector <string> type;
int carNr;

};

// read data funtion
// I done this so far, got errors

void readData (Car & car) {
 cout << "Enter the car type please", getline (cin, car.type);

}
Line 2 means you are able to store for a Car multiple types. "Buick", "Pontiac", "Chevy", "Ford", etc. Or whatever you define a car type as being.

Is that really what you want? I would have "plain" std::string as the Car type since a car has only one type.

1
2
3
4
5
struct Car
{
   std::string type;
   int number;
};


Now your readData function will work as written.
closed account (oj2wbRfi)
The struct must be

1
2
3
4
5
 struct Car {
vector <string> type;
int carNr;

};

but the readData function get errors
so there is error in line 11 above !
Last edited on
Of course you are getting an error, you are not accessing the type vector's element(s) properly.

Do you know how to access a vector's elements? That is the key.
closed account (oj2wbRfi)
access:

the_function_paramtername .* the vector name
https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/

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

struct Car
{
   std::vector<std::string> type;
   int                      number;
};

int main()
{
   Car car;

   std::string temp;

   std::cout << "Enter the car's type: ";
   std::getline(std::cin, temp);
   std::cout << '\n';
   car.type.push_back(temp);
   car.number = 1;

   std::cout << "Car type: " << car.type[0] << '\n';
   std::cout << "Car #:    " << car.number << '\n';
}
Enter the car's type: This is a TEST

Car type: This is a TEST
Car #:    1

Having the car type be a vector of strings may be the requirement, but without knowing EXACTLY what your assignment is, ALL of it, using a vector to store a bunch of types makes ZERO sense. Especially since a car's number is a single entry.
closed account (oj2wbRfi)
thanks for clarifying but Imo not asking for this

my question was about:

1
2
3
4
5
6
7
8
9

struct Car {
vector <string > type;
};

void readData( Car & car) {
// how to take input from user , I mean take the type of car ?
//thats it :)
}

and what's the issue with the idea given above by George?

What data are you wanting to read? Do you mean something like this (use end-of-file to terminate loop (ctrl-z on windows)):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <string>
#include <iostream>

struct Car {
	std::vector <std::string > ctype;
};

void readData(Car& car) {
	for (std::string typ; (std::cout << "Enter car type: ") && std::getline(std::cin, typ); car.ctype.emplace_back(std::move(typ)));
}

int main() {
	Car cars;

	readData(cars);

	for (const auto& t : cars.ctype)
		std::cout << t << '\n';
}

Last edited on
If I have understood the requirement correctly, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <vector>

struct car {

    std::vector<std::string> type_tags ;
};

int main() {

    car a_car ;
    std::cout << "enter words describing the type of the car, end input with END\n" ;

    std::string word ;
    while( std::cin >> word && word != "END" ) a_car.type_tags.push_back(word) ;

    std::cout << "type tags:\n-------\n" ;
    for( const std::string& str : a_car.type_tags ) std::cout << "    " << str << '\n' ;
}
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

#include <iostream>
#include <string>
#include <vector>

struct Car
{
    std::string type;
    int carNr;
};


void readData (Car& car)
{
    std::cout << "Enter the car type please: ";
    std::getline (std::cin, car.type);
    
    std::cout << "Enter the car number please: ";
    std::cin >> car.carNr;
    
    std::cin.ignore(1000, '\n');
}

int main()
{
    std::vector<Car> car_store;
    
    Car temp;
    
    // INPUT & STORE
    for(int i = 0; i < 3; i++)
    {
        readData(temp);
        car_store.push_back(temp);
    }
    
    // OUTPUT STORE CONTENTS
    for(int i = 0; i < car_store.size(); i++)
    {
        std::cout << car_store[i].type << ' ' << car_store[i].carNr << '\n';
    }

    return 0;
}



Enter the car type please: Ford Coupe
Enter the car number please: 1234
Enter the car type please: Buick
Enter the car number please: 99878
Enter the car type please: Chevrolet Impale
Enter the car number please: 888
Ford Coupe 1234
Buick 99878
Chevrolet Impale 888
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.