Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2029
  • Last Modified:

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

0
whully1
Asked:
whully1
  • 3
  • 2
  • 2
6 Solutions
 
Infinity08Commented:
>> string months = months;

>>                 string months = months[startScan];

>>                                 string months = months[index];

>>                 months[startScan] = string months;

I'm not sure what you're trying to do in the above, but they're all definitely wrong.


Also, the title of your question states :

>> How do you convert std::string into an int?

but the body says something else ... Which is your question ?
0
 
Infinity08Commented:
>>         string months[size] = {"January", "February", "March", "April", "May","June","July","August",
>>                                  "September", "October", "November", "December"};

size is defined as an array. If you want to use it as a dimension for an array, it needs to ba a const integer type.


>>                 for(int index = startScan + 1; index < months; index++)

what does index < months mean ? index is an int, and months is an array ... How does comparing an int to an array make sense ?
0
 
itsmeandnobodyelseCommented:
Looks like a selection sort on two arrays running the same index..
void dualSort(double rain[], string months[], int size)
{
	int startScan, maxIndex, strMonths;
	double maxValue;
	string month = month[0];
	for(startScan = 0; startScan < (size - 1); startScan++);
	{
		maxIndex = startScan;
		maxValue = rain[startScan];
		month    = months[startScan];
		for(int index = startScan + 1; index < size; index++)
		{
			if(rain[index] < maxValue)
			{
				maxValue = rain[index];
				month = months[index];
				maxIndex = index;
			}
		}
		rain[maxIndex] = rain[startScan];
		months[maxIndex] = months[startScan];
		rain[startScan] = maxValue;
		months[startScan] = month;
	}
}

Open in new window

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
whully1Author Commented:
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++.
0
 
itsmeandnobodyelseCommented:
>>>> but the errors were saying can not convert string to int
That is cause you used a string variable 'months' in the for loop as upper boundary. Look at my code. I exchanged the 'string months' by month (as you want to safe only the month of the current maximum rain fall) and the upper boundary in the inner for loop by size.  Did you try my code?
0
 
itsmeandnobodyelseCommented:
>>>> Sorry I'm still learning c++.
Nothing to be sorry about ;-)

>>>> string month = month[0];

That was a typo. It should be

   string month = months[0];

and simply initializes the local string variable month with the "january".

Note, in your code there were a lot expressions like

    string months = months;

That is a few flaws in one statement.

First, you see that the variable left of = has the same name than the variable on the right. Though some compilers would tolerate that,  taking the newer variable for next references, you never should do such an assignment.

Second, the left side is a single string while the right side is an array. That gives an error.

Third, for each new 'string months' you define a new variable which is only valid within the current scope (the block quoted by curly brackets). So if you have

   string s = "Hello World";
   for (int i = 0; i < 10; ++i)
   {
       string s = "Welcome";
       cout << s << endl;
   }
   cout << s << endl;

the first output is

   Welcome

and the second is

Hello World

cause the string s always defines a new variable.



0
 
whully1Author Commented:
Thank you, I understand now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now