### Beginner c++ student with question about using strings

I have an assignment where I have to: 1.Display three destination choices for the user to select (one) choice. (Atlanta, Tallulah Falls, Washington, DC).
2.Ask the user and get input for their choice destination
**3.Store the location of the user’s choice in a string variable.** (This is the one I am confused about)
4.Ask and get input for the distance and average speed from Atlanta to their destination
5.Calculate the time it will take to travel to your destination given normal driving conditions.
6.Display the results (traveling time).

Could someone please show me how exactly I would store the location of the user's choice in a string variable?

Below is what I have done so far. I stored the location of the user's choice using a number instead of a string..

#include <iostream>
using namespace std;

int main()
{
// Numbers are assigned to the following destinations.
cout<<"Below are destinations you can visit for spring break (their distances from Macon, Ga are included)"<<endl;
cout<<"1. Atlanta - 83.3 mi"<<endl;
cout<<"2. Tallulah Falls - 174.8 mi"<<endl;
cout<<"3. Washington DC - 665.2 mi"<<endl;

//variables used for destination, time, hours, minutes, average spee d
int d;
d = 0;
double dist;
double avgspeed;
double t;
t = 0;
int m;
int hr;
int min;

//User types in the number assigned to their chosen destination

while (t == 0)
{
cin>>d;
if (d == 1)
{
cout<<"Great choice! Atlanta has a lot to do!"<<endl;
cout<<"Please enter the distance from Macon to Atlanta in mi."<<endl;
cin>>dist;
cout<<"Please enter the average speed you will travel from Macon to Atlanta mi."<<endl;
cin>>avgspeed;
t = dist/avgspeed;
m = t*60;
hr = m/60;
min = m%60;
cout<<"It will take you " <<hr<< "hour(s) and " <<min<< "minute(s) to get to Atlanta."<<endl;
cout<<"Have a safe trip!"<<endl;
}
else if (d == 2)
{
cout<<"Nice! Tallulah Falls has beautiful scenery!"<<endl;
cout<<"Please enter the distance from Macon to Tallulah Falls in mi."<<endl;
cin>>dist;
cout<<"Please enter the average speed you will travel from Macon to Tallulah Falls in mi."<<endl;
cin>>avgspeed;
t = dist/avgspeed;
m = t*60;
hr = m/60;
min = m%60;
cout<<"It will take you " <<hr<< "hour(s) and " <<min<< "minute(s) to get to Tallulah Falls."<<endl;
cout<<"Have a safe trip!"<<endl;
}
else if (d == 3)
{
cout<<"Great choice! DC is a great place to visit!"<<endl;
cout<<"Please enter the distance from Macon to DC in mi."<<endl;
cin>>dist;
cout<<"Please enter the average speed you will travel from Macon to DC in mi."<<endl;
cin>>avgspeed;
t = dist/avgspeed;
m = t*60;
hr = m/60;
min = m%60;
cout<<"It will take you " <<hr<< "hour(s) and " <<min<< "minute(s) to get to DC"<<endl;
cout<<"Have a safe trip!"<<endl;
}
else if (d <= 0)
{
cout<<"INVALID choice. Please enter a number between 1 and 3"<<endl;
}
else if (d >=4 )
{
cout<<"INVALID choice. Please choose a number between 1 and 3"<<endl;
}
}

return 0;
}

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include int main() { // 1.Display three destination choices for the user to select (one) choice. // (Atlanta, Tallulah Falls, Washington, DC). const std::string atlanta = "Atlanta" ; const std::string tallulah = "Tallulah Falls" ; const std::string washington = "Washington, DC" ; std::cout << "destinations:\n-----------\n\n" << "1. " << atlanta << '\n' << "2. " << tallulah << '\n' << "3. " << washington << '\n' ; // 2.Ask the user and get input for their choice destination std::cout << "\nchoice (1/2/3)? " ; int choice ; if( std::cin >> choice && choice >= 1 && choice <= 3 ) { // 3.Store the location of the user’s choice in a string variable. std::string chosen_destination ; // string to hold the users choice of destination // set the value of chosen_destination as appropriate if( choice == 1 ) chosen_destination = atlanta ; else if( choice == 2 ) chosen_destination = tallulah ; else chosen_destination = washington ; std::cout << "you chose '" << chosen_destination << "' as your destnation.\n" ; // TO DO: rest of program // 4.Ask and get input for the distance and average speed from Atlanta to their destination // (from Atlanta? check with your teacher: what if the user chose Atlanta as the destination?) // 5.Calculate the time it will take to travel to your destination given normal driving conditions. // 6.Display the results (traveling time). } else std::cout << "invalid choice.\n" ; // error in input }``````
Ok so I am having an error when I run my program. It keeps saying that "chosen_destination" was not declared in this scope. What can I do to improve on this? Here is what I program looks like after I included the the string steps:

#include <iostream>
#include <string>
using namespace std;

int main()
{
// 1.Display three destination choices for the user to select (one) choice.
// (Atlanta, Tallulah Falls, Washington, DC).
string atlanta = "Atlanta" ;
string tallulah = "Tallulah Falls" ;
string washington = "Washington, DC" ;

//variables used for destination, time, hours, minutes, average spee d
int d;
d = 0;
double dist;
double avgspeed;
double t;
t = 0;
int m;
int hr;
int min;

cout<< "destinations:"<<endl;
cout<< "1. " <<atlanta<<endl;
cout<< "2. " <<tallulah<<endl;
cout<< "3. " <<washington<<endl;

//Ask the user and get input for their chosen destination
cout << "choice (1/2/3)? "<<endl;
int choice;
if( cin >> choice && choice >= 1 && choice <= 3 )
{
// Store the location of the user's choice in a string variable.
// string to hold the users choice of destination
string chosen_dest;

// set the value of chosen_destination as appropriate
if( choice == 1 ) chosen_dest = atlanta;
{
cout << "you chose " << chosen_dest << " as your destination"<<endl ;
cout<<"Great choice! Atlanta has a lot to do!"<<endl;
cout<<"Please enter the distance from Macon to Atlanta in mi."<<endl;
cin>>dist;
cout<<"Please enter the average speed you will travel from Macon to Atlanta mi."<<endl;
cin>>avgspeed;
t = dist/avgspeed;
m = t*60;
hr = m/60;
min = m%60;
cout<<"It will take you " <<hr<< "hour(s) and " <<min<< "minute(s) to get to Atlanta."<<endl;
cout<<"Have a safe trip!"<<endl;
}
}
else if( choice == 2 ) chosen_dest = tallulah;
{
cout << "you chose " << chosen_dest << "' as your destnation"<<endl ;
cout<<"Nice! Tallulah Falls has beautiful scenery!"<<endl;
cout<<"Please enter the distance from Macon to Tallulah Falls in mi."<<endl;
cin>>dist;
cout<<"Please enter the average speed you will travel from Macon to Tallulah Falls in mi."<<endl;
cin>>avgspeed;
t = dist/avgspeed;
m = t*60;
hr = m/60;
min = m%60;
cout<<"It will take you " <<hr<< "hour(s) and " <<min<< "minute(s) to get to Tallulah Falls."<<endl;
cout<<"Have a safe trip!"<<endl;
}
else if( choice ==3 ) chosen_dest = washington;
{
cout << "you chose " << chosen_dest << "' as your destnation"<<endl ;
cout<<"Great choice! DC is a wonderful place to visit!"<<endl;
cout<<"Please enter the distance from Macon to DC in mi."<<endl;
cin>>dist;
cout<<"Please enter the average speed you will travel from Macon to DC."<<endl;
cin>>avgspeed;
t = dist/avgspeed;
m = t*60;
hr = m/60;
min = m%60;
cout<<"It will take you " <<hr<< "hour(s) and " <<min<< "minute(s) to get to DC"<<endl;
cout<<"Have a safe trip!"<<endl;
}

}
else cout <<"invalid choice."<<endl; // error in input

return 0;
}

Last edited on
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include int main() { // 1.Display three destination choices for the user to select (one) choice. // (Atlanta, Tallulah Falls, Washington, DC). const std::string atlanta = "Atlanta" ; const std::string tallulah = "Tallulah Falls" ; const std::string washington = "Washington, DC" ; std::cout << "destinations:\n-----------\n\n" << "1. " << atlanta << '\n' << "2. " << tallulah << '\n' << "3. " << washington << '\n' ; // 2.Ask the user and get input for their choice destination std::cout << "\nchoice (1/2/3)? " ; int choice ; if( std::cin >> choice && choice >= 1 && choice <= 3 ) { // 3.Store the location of the user’s choice in a string variable. std::string chosen_destination ; // string to hold the users choice of destination // set the value of chosen_destination as appropriate if( choice == 1 ) chosen_destination = atlanta ; else if( choice == 2 ) chosen_destination = tallulah ; else chosen_destination = washington ; std::cout << "\nyou chose '" << chosen_destination << "' as your destnation.\n" ; // 4.Ask and get input for the distance and average speed from Macon to their destination const std::string starting_point = "Macon" ; int distance_kms ; std::cout << "\nPlease enter the distance from " << starting_point << " to " << chosen_destination << " in kilometres: " ; if( std::cin >> distance_kms && distance_kms > 0 ) { int kmph ; std::cout << "\nPlease enter the average speed you will travel\n\tfrom " << starting_point << " to " << chosen_destination << " in kilometres per hour: " ; if( std::cin >> kmph && kmph > 0 ) { // 5.Calculate the time it will take to travel to your destination // given normal driving conditions. const int MINUTES_PER_HOUR = 60 ; const double kms_per_minute = double(kmph) / MINUTES_PER_HOUR ; // total_minutes rounded to the nearest minute const int total_minutes = int( distance_kms / kms_per_minute + 0.5 ) ; const int hours = total_minutes / MINUTES_PER_HOUR ; const int minutes = total_minutes % MINUTES_PER_HOUR ; std::cout << "\nIt will take you " ; if( hours > 0 ) std::cout << hours << " hours and " ; std::cout << minutes << " minutes to get to " << chosen_destination << " from " << starting_point << '\n' ; } else std::cout << "invalid average speed\n" ; } else std::cout << "invalid distance.\n" ; } else std::cout << "invalid choice of destination.\n" ; // error in input }``````
As a beginner I would recommend to code slowly and detect vulnerabilities among your code. That's how you ensure ""code security"". You can also use a app to do that. I know one called Checkmarx if you want to try.
Good luck!
Topic archived. No new replies allowed.