whully1
asked on
How do you convert std::string into an int?
I'm writing a program that is to display rainfall amounts, the rainfall amounts is input from a user then we are to display the months and amount in descending order. I thought I had my code written correctly following my book, but i get all these errors:
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(17) : error C2057: expected constant expression
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(17) : error C2466: cannot allocate an array of constant size 0
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(79) : error C2082: redefinition of formal parameter 'months'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(79) : error C2440: 'initializing' : cannot convert from 'std::string []' to 'std::basic_string<_Elem,_ Traits,_Ax >'
with
[
_Elem=char,
_Traits=std::char_traits<c har>,
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(84) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_ Traits,_Ax >'
with
[
_Elem=char,
_Traits=std::char_traits<c har>,
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_T raits,_All oc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_T raits,_All oc> &' from 'int'
c:\program files\microsoft visual studio 9.0\vc\include\string(150) : see declaration of 'std::operator <'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_T raits,_All oc> &)' : could not deduce template argument for 'const _Elem *' from 'int'
c:\program files\microsoft visual studio 9.0\vc\include\string(140) : see declaration of 'std::operator <'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_T raits,_All oc> &,const std::basic_string<_Elem,_T raits,_All oc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_T raits,_All oc> &' from 'int'
c:\program files\microsoft visual studio 9.0\vc\include\string(130) : see declaration of 'std::operator <'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::reverse_iterator<_Ran It> &,const std::reverse_iterator<_Ran It2> &)' : could not deduce template argument for 'const std::reverse_iterator<_Ran It> &' from 'int'
c:\program files\microsoft visual studio 9.0\vc\include\xutility(22 62) : see declaration of 'std::operator <'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Bas e> &,const std::_Revranit<_RanIt2,_Ba se2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Bas e> &' from 'int'
c:\program files\microsoft visual studio 9.0\vc\include\xutility(20 72) : see declaration of 'std::operator <'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'int'
c:\program files\microsoft visual studio 9.0\vc\include\utility(99) : see declaration of 'std::operator <'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2677: binary '<' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(90) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_ Traits,_Ax >'
with
[
_Elem=char,
_Traits=std::char_traits<c har>,
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(97) : error C2275: 'std::string' : illegal use of this type as an expression
c:\program files\microsoft visual studio 9.0\vc\include\xstring(221 0) : see declaration of 'std::string'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(97) : error C2146: syntax error : missing ';' before identifier 'months'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(17) : error C2057: expected constant expression
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(17) : error C2466: cannot allocate an array of constant size 0
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(79) : error C2082: redefinition of formal parameter 'months'
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(79) : error C2440: 'initializing' : cannot convert from 'std::string []' to 'std::basic_string<_Elem,_
with
[
_Elem=char,
_Traits=std::char_traits<c
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(84) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_
with
[
_Elem=char,
_Traits=std::char_traits<c
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_T
c:\program files\microsoft visual studio 9.0\vc\include\string(150)
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_T
c:\program files\microsoft visual studio 9.0\vc\include\string(140)
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_T
c:\program files\microsoft visual studio 9.0\vc\include\string(130)
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::reverse_iterator<_Ran
c:\program files\microsoft visual studio 9.0\vc\include\xutility(22
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Bas
c:\program files\microsoft visual studio 9.0\vc\include\xutility(20
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'int'
c:\program files\microsoft visual studio 9.0\vc\include\utility(99)
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(85) : error C2677: binary '<' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(90) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_
with
[
_Elem=char,
_Traits=std::char_traits<c
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(97) : error C2275: 'std::string' : illegal use of this type as an expression
c:\program files\microsoft visual studio 9.0\vc\include\xstring(221
c:\documents and settings\dad\my documents\visual studio 2008\projects\chapter 8.1 question 5\chapter 8.1 question 5\chapter 8.1 question 5.cpp(97) : error C2146: syntax error : missing ';' before identifier 'months'
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void dualSort(double [], string [], int);
void showArray(double[], string [], int);
int main()
{
int size[12];
string months[size] = {"January", "February", "March", "April", "May","June","July","August",
"September", "October", "November", "December"};
double rain[12];
double total = 0;
double average;
double min = 0;
double max = 0;
for (int i = 0; i <=11; i++)
{
cout << "Enter the amount of rainfall in " << months[i] << ": ";
cin >> rain[i];
while (rain[i] < 0)
{
cout << "Cannot have negative rainfall! Please enter again:";
cin >> rain[i];
}
}
for(int i = 0; i <= 11; i++)
{
total = total + rain[i];
}
average = total / 12;
min = rain[0];
for(int i = 1; i < 12; i++)
{
if(rain[i] < min)
min = rain[i];
}
max = rain[0];
for(int i = 1; i < 12; i++)
{
if(rain[i] > max)
max = rain[i];
}
cout << "Total Rainfall: " << total << endl;
cout << "Average Rainfall: " << average << endl;
cout << "The lowest monthly rainfall is " << min << endl;
cout << "The highest monthly rainfall is " << max << endl;
cout << "\tThe unsorted string is: \n";
showArray(rain, months, 12);
dualSort(rain, months, 12);
cout << "\n\tThe sorted string is: \n";
showArray(rain, months, 12);
system ("pause");
return 0;
}
void dualSort(double rain[], string months[], int size)
{
int startScan, maxIndex, strMonths;
double maxValue;
string months = months;
for(startScan = 0; startScan < (size - 1); startScan++);
{
maxIndex = startScan;
maxValue = rain[startScan];
string months = months[startScan];
for(int index = startScan + 1; index < months; index++)
{
if(rain[index] < maxValue)
{
maxValue = rain[index];
string months = months[index];
maxIndex = index;
}
}
rain[maxIndex] = rain[startScan];
months[maxIndex] = months[startScan];
rain[startScan] = maxValue;
months[startScan] = string months;
}
}
void showArray(double rain[], string months[], int size)
{
for(int count = 0; count < size; count++)
cout << count << ": " << rain[count] << endl;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Basically the program is to display the month with the rainfall input from the user into descending order. My instructor told me to use a dualSort function for this. So I just followed the examples. Is this not a correct way to do this? Sorry I'm still learning c++.