C++ Programming Help


The initial output of my program is:

Dear potential adopter:

What is your full name?
input random name
What is the name of the pet you wish to adopt?
What is the type of pet you wish to adopt?
... (notice how there is no space to input for the statement above the previous)


But, I need it to be:

Dear potential adopter:

What is your full name?
input random name
What is the name of the pet you wish to adopt?
input random name
What is the type of pet you wish to adopt?
input random type
....

what can I do to get that?

my code is below.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main () {
    //Full name of adopter of pet
    string adopter;
    //name of pet wished to be adopted
    string name_of_pet;
    //Type of Pet
    string type_of_pet;
   //weight of pet in pounds
    int weight;

    //Prompts for the above variables/strings from the user
    cout << "Dear potential adopter: " << endl;
    cout << " " << endl;

    cout << "What is your full name?" << endl;
    cin >> adopter;

    cout << "What is the name of the pet you wish to adopt?" << endl;
    cin >> name_of_pet;

    cout << "What is the type of pet you wish to adopt?" << endl;
    cin >> type_of_pet;

    cout << "What is weight(in pounds) of the pet you wish to adopt?" << endl;
    cin >> weight;

//Calculations of Cost to Adopt Pet

    // The variable for the initial cost of adoption of a Pet - always $100.00
    float initial_cost_of_adoption;
        initial_cost_of_adoption = 100.00;

    // The additional cost added by the weight of the pet
    float additional_cost_by_weight;
        additional_cost_by_weight = weight * 0.5;

    //Random medical fee between 20 and 30 dollars
    unsigned seed = time (0);
    srand(seed);
    float medical_fee;
    medical_fee = rand()%10 + 20;

    // Calculation of the Subtotal with all costs and fees
    float subtotal;
    subtotal = initial_cost_of_adoption + additional_cost_by_weight + medical_fee;

    // Calculate Sales Tax at 4.5%
    float sales_tax;
    sales_tax = subtotal * 0.045;

    // Calculate Total
    float total;
    total = subtotal + sales_tax;

cout << "" << endl;
cout << "Thank you for asking about adopting a " << type_of_pet << " at our animal hospital!" << endl;
cout << "" << endl;
cout << adopter << ", the cost to adopt you new " << type_of_pet << name_of_pet << " who is " << weight << " pounds is as follows: " << endl;
cout << "-----------------------------------" << endl;
cout << "Initial cost:              $ " << showpoint << fixed << setprecision (2) << setw(6) << initial_cost_of_adoption << endl;
cout << "Medical fee:               $ " << showpoint << fixed << setprecision (2) << setw(6) << medical_fee << endl;
cout << "Weight fee:                $ " << showpoint << fixed << setprecision (2) << setw(6) << additional_cost_by_weight << endl;
cout << "-----------------------------------" << endl;
cout << "Subtotal:                  $ " << showpoint << fixed << setprecision (2) << setw(6) << subtotal << endl;
cout << "Tax:                       $ " << showpoint << fixed << setprecision (2) << setw(6) << sales_tax << endl;
cout << "-----------------------------------" << endl;
cout << "Total:                     $ " << showpoint << fixed << setprecision (2) << setw(6) << total << endl;
return 0;
}


Last edited on
If the names being entered can be more than one word, try using getline. As you have it now, cin just reads up to a white space.

For example,
getline(cin, adopter); instead of cin >> adopter;
Thanks, it worked.
Topic archived. No new replies allowed.