On the last part of my program, I am trying to convert a string to a double, the code I have compiles but will not run once the program gets to that part of code. What do I need to change to make it work? I included the code to see what I am trying to do.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
usingnamespace std;
int main()
{
vector<string> roomNum;
vector<string> lecName;
vector<string> students;
vector<string> numOfChairs;
vector<string> window;
vector<string> projector;
vector<string> available;
vector<string> numOfStudents;
string quit = "Yes";
while(quit != "No")
{
//Enter classroom number
cout <<"\t\t\tClassroom Information\n";
cout <<"Enter the classroom number\n";
string number;
cin >>ws;
getline(cin, number);
roomNum.push_back(number);
//Enter Lecture name
cout <<"\nEnter the Lecture Name\n";
string name;
cin >>ws;
getline(cin, name);
lecName.push_back(name);
//Enter number of students per room
cout <<"\nEnter the number of students in this room.\n";
string studentNumber;
cin >> ws;
getline(cin, number);
numOfStudents.push_back(studentNumber);
//Enter student names
cout <<"\nEnter all the student names(seperate names by using a (,))\n";
string studentNames;
cin >> ws;
getline(cin, studentNames);
students.push_back(studentNames);
//Enter the number of chairs in a room
cout <<"\nHow many chairs are in this room?\n";
string chairs;
cin >> ws;
getline(cin, chairs);
numOfChairs.push_back(chairs);
//Asks if there are windows in the room
cout <<"\nAre there any windows in the room? (Yes or No)\n";
string windows;
cin>> ws;
getline(cin, windows);
window.push_back(windows);
// Asks if there is a projector in the room
cout <<"\nIs there a projector in this room? (Yes or No)\n";
string proj;
cin >> ws;
getline(cin, proj);
projector.push_back(proj);
//Asks if the room is available
cout <<"\nIs the room available? (Yes or No)\n";
string room;
cin >> ws;
getline(cin, room);
available.push_back(room);
//Control for the while loop
cout <<"\nAre there more classrooms to enter? (Yes or No)\n";
cin >> quit;
}
//Loop to print out all the classroom information
cout << "\nClassroom Information:\n";
for (unsignedint i=0; i < roomNum.size(); ++i)
{
cout <<"Room Number: " << roomNum[i] << endl;
cout <<"Lecture Name: " << lecName[i] << endl;
cout <<"List of Students: " << students[i] << endl;
cout <<"Number of Chairs: " << numOfChairs[i] << endl;
cout <<"Window: " << window[i] << endl;
cout <<"Projector: " << projector[i] << endl;
cout <<"Available: " << available[i] << endl;
cout << endl;
}
//compare two classroom based on the number of chairs
string roomNum1;
string roomNum2;
string numOfChairs1;
string numOfChairs2;
string numOfStudents1;
string numOfStudents2;
cout <<"Compare two rooms by number of chairs.\n";
cout <<"\nEnter the first room number.";
cin >>roomNum1;
cout <<"\nEnter second room number.";
cin >>roomNum2;
//Loop to find the room number and get the number of chairs in that room
for (unsignedint i=0; i < roomNum.size(); ++i)
{
if(roomNum[i]== roomNum1)
{
cout <<"\nRoom number "<< roomNum1 << " has " << numOfChairs[i] <<" number of chairs in it.\n";
numOfChairs1 = numOfChairs[i];
numOfStudents1 = numOfStudents[i];
}
elseif(roomNum[i]== roomNum2)
{
cout <<"\nRoom number "<< roomNum2 << " has " << numOfChairs[i] <<" number of chairs in it.\n";
numOfChairs2 = numOfChairs[i];
numOfStudents2 = numOfStudents[i];
}
}
//Displays what room has the most number of chairs
if(numOfChairs1 > numOfChairs2)
{
cout <<"\nRoom number "<< roomNum1 <<" has more chairs than room number " << roomNum2 <<".\n";
}
elseif(numOfChairs2 > numOfChairs1)
{
cout <<"\nRoom number "<< roomNum2 <<" has more chairs than room number " << roomNum1 <<".\n";
}
//Displays utilization
double student1;
double chair1;
double student2;
double chair2;
//Switches a string to a double
student1 = stod(numOfStudents1);
chair1 = stod(numOfChairs1);
student2 = stod(numOfStudents2);
chair2 = stod(numOfChairs2);
//Displayes the percentage of chairs being used in the to rooms being compaired
cout <<"\nRoom number " << roomNum1 <<" has a utilization of "<< student1/chair1 <<"%.";
cout <<"\nRoom number " << roomNum2 <<" has a utilization of "<< student2/chair2 <<"%.";
}
jlb, I am very new at programming and this is the only way I know how to use vectors at the moment. If there is anything you can show me that make this program easier would be greatly appreciated.
Hey I found a way to parse from string to int on the references in this site and it shows an example like this...
1 2 3 4
int parseInt(string arg){
int num
return (istringstream(arg) >> num) ? num : 0;
}
You will need to include the <sstream> header for this to work as it manipulates the istringstream... I do notice you said double and not int but I'm sure that this should be transferable (sorry if it's not)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int parseInt(string arg){
int num
return (istringstream(arg) >> num) ? num : 0;
}
float parseFloat(string arg){
float num
return (istringstream(arg) >> num)) ? num : 0;
}
double parseDouble(string arg){
float num
return (istringstream(arg) >> num)) ? num : 0;
}