Thread: Beginner Question

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Beginner Question

    How do I fix this? I don't know how to convert inputted choice to equal the If statement.

    Code:
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    
    using namespace System;
    
    int _tmain()
    {
    	double cont = 1, r, theta, x, y, A, B;
        __wchar_t choice;
    	while (cont == 1)
    	{
       	Console::Write( S"A	Convert Polar Coordinates to Cartesian \n");
    	Console::Write( S"B	Convert Cartesian Coordinates to Polar \n");
    	Console::Write( S"C	Determine Roots of a quadratic equation \n");
    	choice = Char::Parse(Console::ReadLine());
    
       if ( choice == A)
       {
        Console::Write( S"R: ");
        r = Int32::Parse( Console::ReadLine());
    	Console::WriteLine( S"Theta: ");
    	theta = Int32::Parse( Console::ReadLine() );
    	x = r * Math::Cos(theta);
    	y = r * Math::Sin(theta);
    	Console::Write( S"X: {0}", x.ToString());
    	Console::Write( S"Y: {0}", y.ToString());
    	return 0;
       }
       if ( choice == B)
       {
        Console::WriteLine( S"X: ");
    	x = Int32::Parse( Console::ReadLine() );
    	Console::WriteLine( S"Y: ");
    	y = Int32::Parse( Console::ReadLine() );
    	r = Math::Sqrt(Math::Pow(x, 2)+Math::Pow(y, 2));
    	theta = Math::Atan(y/x);
    return 0;
       }
    	}
    }

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Methinks this is C#. This is a C++ forum. Mods, please move.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    This is C++ with .net

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
       if ( choice == __wchar_t('A'))
    .
    .
       elseif ( choice == __wchar_t('B'))

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Oh, OK. My fault, I haven't used .NET in so long.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Quote Originally Posted by swoopy
    Code:
       if ( choice == __wchar_t('A'))
    .
    .
       elseif ( choice == __wchar_t('B'))
    Thanks for the help but, inputting A still doesn't bring up thing.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Nevermind it works. Thanks!

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    What does it mean when it says unreferenced local variable, (A and B)?

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    It means you didn't "use" A or B.
    Code:
      double cont = 1, r, theta, x, y, A, B;
        __wchar_t choice;
            while (cont == 1)
            {
            Console::Write( S"A     Convert Polar Coordinates to Cartesian \n");
            Console::Write( S"B     Convert Cartesian Coordinates to Polar \n");
            Console::Write( S"C     Determine Roots of a quadratic equation \n");
            choice = Char::Parse(Console::ReadLine());
    
       if ( choice == A)
    Maybe the compiler is warning you of this undefined behavior. Technically, the variable is referenced by the == operator, no? I don't know. Initialize your variables.
    Last edited by CodeMonkey; 01-23-2007 at 07:44 PM. Reason: preposition error
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Hi, thanks for all the answers.. one more question

    Code:
       if ( choice == __wchar_t('A'))
       {
        Console::Write( S"R: ");
    	r = Double::Parse( Console::ReadLine());
    	Console::Write( S"Degrees: ");
    	degree = Double::Parse( Console::ReadLine() );
    	rad = degree  * (Math::PI/180);
    	Console::WriteLine("");
    	x = r * Math::Cos(rad);
    	y = r * Math::Sin(rad);
    	Console::WriteLine( S"X: {0}", x.ToString());
    	Console::WriteLine( S"Y: {0}", y.ToString());
       }
    Do you guys see anything wrong with this?
    It seems to work fine until X or Y is suppose to equal 0.. it gives some e-16 value.

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Hm. Your trig funtions are either returning terribly small numbers, or you're having floating point issues. To debug, every time a double is assigned, print out its value right afterwards to make sure things are what you expect them to be.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Everything is fine until:

    Code:
    	
    x = r * Math::Cos(rad);
    y = r * Math::Sin(rad);
    Where it works except when rad is say PI..

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    How do you cut off a Double variable after say 2 digits?

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How do you cut off a Double variable after say 2 digits?
    In standard C++, you can do this:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
       double num = 5.12345;
    
       std::cout << std::setprecision(2) << std::fixed << num << '\n';
    }
    I have no idea how to do it in .NET.

    Also just so you know:
    Code:
       if ( choice == __wchar_t('A'))
    I'm not positive, but I believe you can write this as:
    Code:
       if ( choice == T('A'))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM