Solved

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

Posted on 2009-03-30
7
1,946 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
Comment Utility
>> 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
Comment Utility
>>         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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:whully1
Comment Utility
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
Comment Utility
>>>> 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
Comment Utility
>>>> 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
Comment Utility
Thank you, I understand now.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 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

10 Experts available now in Live!

Get 1:1 Help Now