-7

Trying to find out how to take an input and check if it is valid, If valid the answer get converted to int in main then passed into my switches. but, I keep getting an error. how can I do this right or is this just dead on arrival. Also is there a much shorter way to get the same result? I am new to programming any help will be appreciated. This is the body of classes.

#include "stdafx.h"
#include "Switchs.h"



Switchs::Switchs()
{

}


Switchs::~Switchs()
{
}

 std::string Switchs::GetCase_Choice() // for retrieving the valid result  
{
    return Right_Choice;
}
//is equal to one override Right_Choice with result. if false pass to next case 
 std::string Switchs::Case_1(std::string Answer) 
{
    if (Answer = 1)
    {
        Right_Choice =Answer;

    }
    else
    {
        Case_2 (Answer);
    }


    return 0;
}

std::string Switchs::Case_2 (std::string Answer)
{
    if (Answer = 2)
    {
        Right_Choice = Answer;
    }
    else
    {
        Case_3 (Answer);
    }
    return Answer;
}

std::string Switchs::Case_3 ( std::string Answer)
{
    if  (Answer = 3)
    {
        Right_Choice = Answer;
    }
    else
    {
        Case_Error (Answer);
    }
    return 0;
}
//if answer is not equal to above cases prompt them to reenter an answer then //enter code here pass that answer into Case_1
std::string Switchs::Case_Error (std::string Answer)
{


    if (false)
    {
        std::string Choice_Num;
        std::cout << "Please enter a number.";
        std::cin >> Choice_Num;
        Case_1(Choice_Num);
    }

    return 0;
}
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • 5
    ...Ouch.. \*robs my eyes\*... While you await help, please pick one of [`these Books and Read`](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) . BTW, Why didn't you use a simple `if-else` ladder? – WhiZTiM Jul 04 '16 at 18:38
  • `if (false)` Huh?? You have all compiler warnings on, have you? – πάντα ῥεῖ Jul 04 '16 at 18:41
  • I agree with WhiZTim, this seems like pretty complicated code to do something trivial. Also the last function `Switchs::Case_Error(...)` does nothing useful. It simply returns 0. Is this some kind of obfuscation? – DBX12 Jul 04 '16 at 18:42
  • _"how can I do this right or is this just dead on arrival. Also is there a much shorter way to get the same result?"_ `if(str == "xxx") { /* doXXX() */ } else if(str == "yyyy") { /* doYYY) */ } else { /* doError() */ }` is the right way to go with strings, since these can't be used as a variable in a `switch()` statement. – πάντα ῥεῖ Jul 04 '16 at 18:45
  • I have only been studying programming for a few mouth now so there is a lot of thing i still need to learn. i had case switches that i tried to loop but did not have the result i was looking for. – David Rhodes Jul 04 '16 at 18:49
  • @DavidRhodes There are so many basic errors in your program that I get the impression that you were just studying a _few mouth_ ful of beer the recent months. Please refer to your textbook and ask your fellow students for help what you've probably been missing. – πάντα ῥεῖ Jul 04 '16 at 18:54
  • @WhiZTiM: Why would you steal your own eyes? – Lightness Races in Orbit Jul 04 '16 at 18:58

1 Answers1

0

I don't really understand what are you asking for, but I'll give you some help:

1) Code like if (Answer = 1) is totally wrong (but it's compilable), because for sure you want to compare Answer with the number 1, not to assign 1 to Answer and then use it as the condition of the if statement. To compare two values, use == Example:

if (Answer == 1)

2) Code inside if (false) will never be executed. I don't know what you wanted to do, but surely not to skip these instructions.

3) Why do you return 0 from functions with std::string as return type? This in non-sense.

I suggest you to read some basic programming books, especially before diving into OOP (!)

EDIT: If you just wanted to check if a number is contained in a range (lets say between 1 and 1337), just do:

int n;
std::cin >> n;
if (n > 0 && n < 1338)
{
 /* do whatever you want */
}
else
{
 /* number is not in the range */
}
Mattia F.
  • 1,720
  • 11
  • 21