Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Alright guys I only come here as a last resort (as in I spend countless hours trying to find the answer in Google) when i have a big idea to make a program to further my understanding of C#. I am stuck in the following scenario: The user will input 2 values for a, b or c. The user can input a and b, b and c, or a and c. Now if the user inputs a and does not know b (here he would put and x, this is where I am having trouble) so the program would take the x and move on to ask for the c value, then with simple math the console would spit out the value for b.

To sum up my problem I am confused as to how C# takes the input of x , moves on to ask for the other value and then would find what x is.

Very sorry that I may be wasting some time with this question but I just started learning this language.

Thanks
-G
Posted

Or this : (asuming that you are doing a + b = c )
actually it's pretty easy to change to a*b =c or whatever :)
C#
class Program
{
    static void Main(string[] args)
    {

       string[]  vars = new string[3];
       vars[0] = Program.getRetVal("A = ");
       vars[1] = Program.getRetVal("B = ");
       vars[2] = Program.getRetVal("C = ");
       int index = 0;
        while (vars[index] != "x")
            index++;

        if (index == 2)
        {
            Console.WriteLine("Answer:" +
                              Convert.ToString(Convert.ToInt32(vars[0]) +
                                               Convert.ToInt32(vars[1])));
        }
        else
        {
              Console.WriteLine("Answer:" +
              Convert.ToString(Convert.ToInt32(vars[2]) -
                               Convert.ToInt32(vars[Convert.ToInt32(!Convert.ToBoolean(index))])));
        }
        Console.ReadKey();
    }
   static private string getRetVal(string info)
    {
        Console.Write(info);
        return  Console.ReadLine().ToLower();
    }
}


the only tricky part for a newcomer here is my casting of an integer to an inverted boolean :cool:
 
Share this answer
 
Comments
Dalek Dave 2-Dec-10 17:36pm    
Good Answer.
Well, there can be various ways. What it sounds like you will have an equation based on a,b & c. Once you have any two value, you will show the third variable value.

Now, few things, if the user knows the equation, you can show and ask for specific values. Lets assume, user doesnt and he will just input any two variable values. Such case, use 'var' as the datatype for all a,b & c. When you get any two variables as proper integer/floating value (you can find that using objects datatype), use those for your calculation and then find the value for missing one. This way, you give flexibility to user to enter x for an integer value and internally you ignore it.
 
Share this answer
 
You can design it, so that for each of the input requests the user can either enter a number or leave blank or enter X (if console, just hit enter). For each line you can use:
Int32.TryParse() 

(or whatever relevant type you use) on your input values to see if it parses to a number. If so, take it, if not, move to the next if 2 haven't been properly parsed yet.
 
Share this answer
 
v2
try with
C#
String a, b, c;
Int32 aInt, bInt, cInt;
Console.WriteLine("Input a:");
if (Int32.TryParse(Console.ReadLine().ToString(), out aInt)) 
{ a = aInt.ToString(); }
else { a = "x" }
Console.WriteLine("Input b:");
if (Int32.TryParse(Console.ReadLine().ToString(), out bInt)) 
{ b = bInt.ToString(); }
else { b = "x" }
if (b == "x" && a == "x")
{
    Console.WriteLine("You can't let 2 var empty");
    return;
}
else if (Int32.TryParse(a, out aInt) && Int32.TryParse(b, out bInt))
{
    c = "x";
}
else
{
    Console.WriteLine("Input c:");
    if (Int32.TryParse(Console.ReadLine().ToString(), out cInt)) 
    { 
        c = cInt.ToString(); 
    }
    else { c = "x" }
}

if (c == "x" && b == "x" || c == "x" && a == "x")
{
    Console.WriteLine("You can't let 2 var empty");
    return;
}
else
{
    //do your stuffs
}

In the "//do your stuffs" I really don't know what you need to do with the two ints and the "x" value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900