Problem with else on strings

This is a discussion on Problem with else on strings within the C++ Programming forums, part of the General Programming Boards category; First of all, I would like to thank to all who helped me first two previous threads.... You guys helped ...

  1. #1
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37

    Angry Problem with else on strings

    First of all, I would like to thank to all who helped me first two previous threads.... You guys helped a lot. Sorry for not posting in my own last thread as if I didn't bother about your posts, but I did read them, I was too busy and just decided to give up on downloading Dev C++ and whatever but instead planing to buy a proper Microsof Visual C++.

    For now, I mannaged to test out my programme, but I have a problem with the else

    Here's the programme:
    Code:
    #include <iostream>
    #include <string>
    
    main ()
    
    {
    	int quantity, total;
    	std::string s;
    	std::cout << "Fruits acailable: Apple, Banana and Orange.\n";
    	std::cout << "Enter type of fruits: ";
    	std::cin >> s;
    
    	if (s == "Apple")
    	{
    		std::cout << "Unit per price is 10cents.\n";
    		std::cout << "Enter quantity: ";
    		std::cin >> quantity;
    		total = quantity * 10;
    		std::cout << "Your total amount is " << total << "cents\n";
    	}
    
    	if (s == "Banana")
    	{
    		std::cout << "Unit per price is 8cents.\n";
    		std::cout << "Enter quantity: ";
    		std::cin >> quantity;
    		total = quantity * 8;
    		std::cout << "Your total amount is " << total << "cents\n";
    	}
    
    	if (s == "Orange")
    	{
    		std::cout << "Unit per price is 15cents.\n";
    		std::cout << "Enter quantity: ";
    		std::cin >> quantity;
    		total = quantity * 15;
    		std::cout << "Your total amount is " << total << "cents\n";
    	}
    
    	else
    	{
    	std::cout << "Fruit entered not available" << std::endl;
    	}
    
    	return 0;
    
    }
    I did use cents here, but I'm planning to use dollars instead to avoid decimal places.
    The problem is the else.

    If I choose Orange it works perfectly fine. But if I use Apple and Banana, the "Fruit entered not available" still appears. Even after giving the total amount.

    I even tried
    Code:
    	else if (s != "Apple", s != "Banana", s != "Orange")
    	{
    	std::cout << "Fruit entered not available" << std::endl;
    	}
    
    	return 0;
    
    }
    The same thing happens





    I was also suggested to use
    Code:
    transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
    so that upper and lower case doesn't differ to the programme, but I'm not sure how to use it. Do I type int the exact code just before the return 0;
    like this?
    Code:
    ....
    ...
    ..
    	}
    
    	transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
    
    	return 0;
    
    }
    is this correct?...

    Also I was thinking of dealing with decimals, but... I don't think the lecturer expects that in our assignment because he hasn't teach us yet... But if it is simple to learn, I might consider using it.


    But my main question here is how do I get the else to work?

    Thank you,

    [Z-D]

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    if (s == "Apple")
    {
    }
    else if (s == "Banana")
    {
    }
    else if (s == "Orange")
    {
    }
    else
    {
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Do I type int the exact code just before the return 0;
    No, that code transforms the string entered by the user to all lower case. You would want to do that before you compare it to the strings in your if/else if statements. You'd also want to #include <algorithm> for transform and call it std::transform. Finally, you would want to add a #include for tolower, but I don't remember what header that one is in. Note that the call to transform is probably pretty advanced for your class, so make sure you understand basically what is going on before you turn in an assignment that uses it.

  4. #4
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Thank you guys. else is working. I won't have any problems with that anymore. I also found out how to deal with decimals. Which was really easy.

    about the #include<algorith> and std::transform... how do I use it?

    Also I decided that I wouldn't give a ... care.. what my lecturer would think if I hand in something too advanced. Coz he doesn't teach us well, he's got problem explaining.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You would put that call to transform right after the input. so that the userinput would become all lowercase characters. If you change the string literals in the ifs to all lowercase as well then your program would work no matter how the user inputs the answer.
    e.g. transform would change the input "aPpLe" to "apple" and the comparison input == "apple" would work.
    Kurt

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could do it in a loop as a matter of fact; the transform() probably does the same thing as well.

    Code:
    for(size_t i = 0; i < string.size(); ++i)
    {
        string[i] = std::tolower(string[i]);
    }
    //now your entire string is tolower()-ed
    //and you can proceed regardless whether your user accidentally left caps lock on
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Thanks a lot guys. The due date for my assignment is today and everything is perfect.

    I didn't use transformation though. I know how it works but still unsure how to use it, as in what to write to use the code. Can someone help me with this


    I don't understand how to use this
    Code:
    for(size_t i = 0; i < string.size(); ++i)
    {
        string[i] = std::tolower(string[i]);
    }
    //now your entire string is tolower()-ed
    //and you can proceed regardless whether your user accidentally left caps lock on
    Can someone explain this.... Its so complex for me casue I just started learning C++ for a month. I don't really listen to the lecturer cause he really doesn't know how to explain. All we do is read what is in the projector and try our best to apply it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21