Link to home
Start Free TrialLog in
Avatar of whully1
whully1Flag for United States of America

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<char>,
            _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<char>,
            _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,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' 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,_Traits,_Alloc> &)' : 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,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' 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<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'int'
        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2262) : 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,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'int'
        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2072) : 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<char>,
            _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(2210) : 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'
#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;
}

Open in new window

SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of whully1

ASKER

I was following the book in this program for a dualSort function, but the errors were saying can not convert string to int, and when I changed it to const int size[12], can not convert char to string.
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++.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial