How to store something in a string without user input?

Hi! I am working on another project, and got stuck...again. This time, my problem is that I want to store something in a string without user input. I have marked the lines that are giving me problems. Thanks guys!

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
 #include <iostream>
#include <string>
using namespace std;

int main()

{
    int Vehicles;
    double Standard;
    double Miles;
    int Emission{};
    int FinishedVehicles{};
    string Permission;
    int VehicleNumber;
    VehicleNumber = 1;

    cout << "Hello! Please enter the carbon monoxide emission standard. ""\n";
    cin >> Standard;
    cout << "Please enter the number of vehicles to be tested. ""\n";
    cin >> Vehicles;
    cout << "Please enter the number of miles the vehicles were driven. ""\n";
    cin >> Miles;
       
    while (FinishedVehicles < Vehicles)
    {
        cout << "Enter total amount of carbon monoxide emitted in grams for vehicle " << VehicleNumber << "\n";
        cin >> Emission;
        if (Emission / Miles > Standard) // From here...
        {
            cout << "exceeds " "\n";
            cin >> Permission;
        }
        if (Emission / Miles < Standard || Emission / Miles == Standard)
        {
            cout << "meets" << "\n";
            cin >> Permission; // To here.
        }
        cout << "Carbon monoxide emission of " << Emission / Miles << " g/mi " << Permission << " permitted emission of " << Standard << "g/mi""\n";
        FinishedVehicles++;
        ++VehicleNumber;
    }
    return 0;
}
Assign the value:

Permission = "exceeds ";

With this:
if (Emission / Miles < Standard || Emission / Miles == Standard)

can be written more concisely as :

if (Emission / Miles <= Standard)

I would prefer Emission to be a double, it won't make any difference here, it just saves the compiler an implicit conversion from int to double in the expression Emission / Miles

Also:

int VehicleNumber {1}; // declare and assign in 1 statement

There is no need to put extra quotes around \n

cout << "Hello! Please enter the carbon monoxide emission standard. \n";
Last edited on
Maybe something like this?

28
29
30
31
32
33
34
35
      if (Emission / Miles > Standard) // From here...
      {
         Permission = "exceeds";
      }
      if (Emission / Miles < Standard || Emission / Miles == Standard)
      {
         Permission = "meets";
      }

A quick test, not knowing what your input is supposed to be:
Hello! Please enter the carbon monoxide emission standard.
4
Please enter the number of vehicles to be tested.
4
Please enter the number of miles the vehicles were driven.
450
Enter total amount of carbon monoxide emitted in grams for vehicle 1
12
Carbon monoxide emission of 0.0266667 g/mi meets permitted emission of 4g/mi
Enter total amount of carbon monoxide emitted in grams for vehicle 2
500
Carbon monoxide emission of 1.11111 g/mi meets permitted emission of 4g/mi
Enter total amount of carbon monoxide emitted in grams for vehicle 3
50000
Carbon monoxide emission of 111.111 g/mi exceeds permitted emission of 4g/mi
Enter total amount of carbon monoxide emitted in grams for vehicle 4
152
Carbon monoxide emission of 0.337778 g/mi meets permitted emission of 4g/mi
the code is a little shorter if you have defaults.

string per = "exceeds"; //it will be this value
if(whatever) //unless you need to change it?
per = "meets";
1
2
const auto emission_per_mile = Emission / Miles ; 
const std::string perm = emission_per_mile  <= standard ? "meets" : "exceeds" ;
There's no need to use string:

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

int main() {
	int Vehicles {};
	double Standard {};
	double Miles {};

	std::cout << "Hello!\nPlease enter the carbon monoxide emission standard: ";
	std::cin >> Standard;

	std::cout << "Please enter the number of vehicles to be tested: ";
	std::cin >> Vehicles;

	std::cout << "Please enter the number of miles the vehicles were driven: ";
	std::cin >> Miles;

	for (int VehicleNumber {1}; VehicleNumber <= Vehicles; ++VehicleNumber) {
		double Emission {};

		std::cout << "\nEnter total amount of carbon monoxide emitted in grams for vehicle " << VehicleNumber << ": ";
		std::cin >> Emission;

		const auto epm {Emission / Miles};

		std::cout << "Carbon monoxide emission of " << epm << " g/mi " << (epm <= Standard ? "meets" : "exceeds") << " permitted emission of " << Standard << "g/mi\n";
	}
}



Hello!
Please enter the carbon monoxide emission standard: 4
Please enter the number of vehicles to be tested: 4
Please enter the number of miles the vehicles were driven: 450

Enter total amount of carbon monoxide emitted in grams for vehicle 1: 12
Carbon monoxide emission of 0.0266667 g/mi meets permitted emission of 4g/mi

Enter total amount of carbon monoxide emitted in grams for vehicle 2: 500
Carbon monoxide emission of 1.11111 g/mi meets permitted emission of 4g/mi

Enter total amount of carbon monoxide emitted in grams for vehicle 3: 50000
Carbon monoxide emission of 111.111 g/mi exceeds permitted emission of 4g/mi

Enter total amount of carbon monoxide emitted in grams for vehicle 4: 152
Carbon monoxide emission of 0.337778 g/mi meets permitted emission of 4g/mi

Thank you so much guys, this was super helpful.
Topic archived. No new replies allowed.