Using tolower on a string

Pages: 12
Or, you could make sure it is properly typed.
http://www.cplusplus.com/forum/beginner/25590/#msg135984
That will fix your error message.
Hi guys,

The error is gone, and it compiles normally :D
However, if I'd put the statement on it's own, the program neglects ans in the ifstatement. And if I put it in the function (which now also works :) the compiler states that you can't return a string value to an int value (which makes sense)..
So now the last problem (for now) is: how can I specify to the program that it's supposed to return to a string value and not an int value?


1
2
3
4
5
6
7
8
9
10
11
12
 	std::string Lower_Ans(std::string ans);
{
	std::transform( ans.begin(), ans.end(), ans.begin(), std::ptr_fun <int, int> ( std::tolower ) );
	return ans;
}

    

    if((ans =="yes")||(ans=="y"))
    {cout<<"\nProgram says: That's great! I'm fine to :)"<<endl;}  //if yes say me 2
    else{cout<<"Program says: Don't worry, it'll be allright!"<<endl;} //if no, encourage user

Can you show us your error and the latest code you are using?
Agreed, the code your showing us does not demonstrate a problem similar to what you are describing, and having the exact error message would be helpful.
Ok guys, so here's the full code I'm using now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

using std::cout;
using std::cin;
using std::getline;
using std::tolower;
using std::endl;
using std::string;
using std::transform;

int main (int argc, char * argv[])
{
    std::string name;
    cout<<"Program says: Hi, what's your name?"<<endl;  //q for name
    cout<<"User says: ";
    std::getline (cin,name); //get name

    cout<<"\nProgram says: Hi "<<name<<", are you fine?"<<endl; //how are you?
    cout<<"User says: ";
    string word;
    string ans;
    getline(cin,word);

#include <functional>  // ptr_fun<>

/*
std::transform( ans.begin(), ans.end(), ans.begin(), std::ptr_fun <int, int> ( std::tolower ) );
*/
 	std::string Lower_Ans(std::string ans);
{
	std::transform( ans.begin(), ans.end(), ans.begin(), std::ptr_fun <int, int> ( std::tolower ) );
	return ans;
}

    
    
    /*if((ans == "yes")||(ans =="Yes")||(ans=="YES")||(ans=="y")||(ans=="Y"))
    if((ans == "yes")||(ans=="y"))*/ // <-- the back up which I used before..

    if((ans =="yes")||(ans=="y"))
    {cout<<"\nProgram says: That's great! I'm fine to :)"<<endl;}  //if yes say me 2
    else{cout<<"Program says: Don't worry, it'll be allright!"<<endl;} //if no, encourage user

/* after this part the code is untouched, and not using the new function (just using the old one I had, before applying the new function to all)*/

    cout<<"\nProgram says: So tell me "<<name<< ", do you like today's weather too?"<<endl; //do you like the weather?
    cout<<"User says: ";
    getline(cin,ans);
    if((ans == "yes")||(ans =="Yes")||(ans=="YES")||(ans=="y")||(ans=="Y"))
    {cout<<"\nProgram says: Cool!";} //if yes, say cool :)
    
    cout<<" So what kind of weather do you like?\nCloudy, Sunny, Rainy, Windy, Humid or Dry? "; //what kind of weather?
    string weth;
    getline (cin,weth);
    
    if((weth == "cloudy")||(weth =="Cloudy")||(weth=="CLOUDY"))
    {cout<<"\nPrgoram says: I guess it's okay if it's cloudy sometimes"<<endl;} //if cloudy, say:
    
    else if((weth == "sunny")||(weth=="SUNNY")||(weth=="Sunny"))
    {cout<<"\nProgram says: I love it when it's sunny!"<<endl;}//if sunny say:
    
    else if((weth == "windy")||(weth == "Windy")||(weth=="WINDY"))//if windy do:
    {
         cout<<"\nProgram says: I only like that when it's hot.. You too?"; //say:
         getline (cin,ans);
         if((ans == "yes")||(ans =="Yes")||(ans=="YES")||(ans=="y")||(ans=="Y"))
                {cout<<"\nProgram says: Great! That's something we have in common then!"<<endl;} //if yes say:
         else {cout<<"\nProgram says: Ah well, people differ"<<endl;} //if no say:
    }
    
    else if((weth=="Humid")||(weth=="humid")||(weth=="HUMID"))
    {cout<<"\nProgram says: I realy don't like it when it's humid.."<<endl;}
    
    else if((weth=="dry")||(weth=="DRY")||(weth=="Dry"))
    {cout<<"\nProgram says: I guess it's okay when it's dry, as long as it isn't too hot"<<endl;}
    
    system("Pause");         // To do: make a good press enter to continue function!
 return 0;   
}


The error message the compiler gives is:


In function `int main(int, char**)':
44: error: cannot convert `std::string' to `int' in return
Execution terminated
You need to move line 27 to the beginning of the file with all the other includes.

Lines 32-36 declare a function, that needs moving outside and before the main function. You can't declare functions within functions in C++;
Ok, so I've moved the function outside main (btw why can't you declare a function in main?).And made a call to it before the if statement (
1
2
3
std::string Lower_Ans;
    if((ans =="yes")||(ans=="y"))
    {cout<<"\nProgram says: That's great! I'm fine to :)"<<endl;}  //if yes say me 2 
).

So now it compiles good, but the problem I had before, is still there..

If you enter "Yes", the output will be:


Program says: Hi kaduuk, are you fine?
User says: Yes
Program says: Don't worry, it'll be allright!


whereas that last sentense should be "That's great, I'm fine too"..
And when I remove the rest of the program, and just tell it to cout<<ans after it converts it,
it doesn't show anything :S

I'm sorry for my noobness, but if we could fix this, it'll help me a lot with some other programs too :)

Thanks!
This is not how you call the function:

1
2
3
std::string Lower_Ans;
    if((ans =="yes")||(ans=="y"))
    {cout<<"\nProgram says: That's great! I'm fine to :)"<<endl;}  //if yes say me 2  


You need to pass in the string you want to convert and use the return value like this:


1
2
3
ans = Lower_Ans(ans); // put ans into function and re-set it to the result
    if((ans =="yes")||(ans=="y"))
    {cout<<"\nProgram says: That's great! I'm fine to :)"<<endl;}  //if yes say me 2  

Thanks so much guys!

It worked :D

The program now runs and compiles fine, and I've learnt a lot thanks to you!

Thanks again!

:)
Topic archived. No new replies allowed.
Pages: 12