convert a string to a double

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.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace 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 (unsigned int 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 (unsigned int 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];
		}

		else if(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";
	}

	else if(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  <<"%.";
}

Why are you trying to store everything in a string in the first place?

Have you considered using a vector of a structure to hold this data instead of the individual vectors?

> will not run once the program gets to that part of code.

Is an exception being thrown? If yes, one or more of your strings do not hold valid numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
try
{
    //Displays utilization

    //Switches a string to a double
    const double student1 = stod(numOfStudents1) ;
    const double chair1 = stod(numOfChairs1);
    const double student2 = stod(numOfStudents2);
    const double 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 * 100 / chair1  << " %.\n" ;
    cout << "\nRoom number " << roomNum2 << " has a utilization of "
         << student2 * 100 / chair2  << " %.\n" ;
}

catch( const std::exception& )
{
    std::cerr << "error in convering string to double\n" ;
    // ...
}
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.
JLBorges, The exception is being thrown so I am guessing something need to be changes with the way I am storing my data.
In
1
2
3
4
5
	//Loop to find the room number and get the number of chairs in that room
	for (unsigned int i=0; i < roomNum.size(); ++i)
	{
               // ....
	}

what would happen if you do not find the room number?

Compute and display the percentage only if you have found the room.
I often like to do string->number conversions like this:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <sstream>

double str2doub(std::string str)
{
  double d;
  std::stringstream iss(str);
  iss >> d;
  return d;
}


Something more generic may look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <type_traits>
#include <iostream>
#include <sstream>

template<class T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type 
    str2Num(std::string str) 
{
  T t;
  std::stringstream iss(str);
  iss >> t;
  return t;
}
 
template<class T>
typename std::enable_if<std::is_integral<T>::value, T>::type 
    str2Num(std::string str) 
{
  T t;
  std::stringstream iss(str);
  iss >> t;
  return t;
}
@Stewbond. I use the same way to convert from string to number.

But why aren't you using a check to see if the stringstream has worked?

i.e.
1
2
3
4
5
6
7
8
9
bool stringToNumber( std::string str, double &d )
{
    std::stringstream ss( str );

    if( ss >> d )
        return true;

    return false;
}
have you tried atof() instead. it's in cstdlib
tbh I'd use boost's lexical_cast for this. It's quicker than using a stringstream and provides exception handling.

e.g
1
2
3
4
#include <boost/lexical_cast.hpp>

string input = "1e-10";
double output = boost::lexical_cast<double>(input);
Last edited on
@arcticman452
Ignore the last few posts as white noise.

Your original approach of using std::stod() is superior to any of those suggestions.
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;
}
Last edited on
Topic archived. No new replies allowed.