Solved

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

Posted on 2009-03-30
7
1,953 Views
Last Modified: 2012-05-06
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
Comment
Question by:whully1
  • 3
  • 2
  • 2
7 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 24019226
>> 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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 24019253
>>         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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 400 total points
ID: 24019662
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:whully1
ID: 24019890
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 400 total points
ID: 24020171
>>>> 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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 400 total points
ID: 24020291
>>>> 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
 

Accepted Solution

by:
whully1 earned 0 total points
ID: 24020650
Thank you, I understand now.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video teaches viewers about errors in exception handling.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now