Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,376 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,610 people online right now. Registration is fast and FREE... Join Now!




If Statement triggering no matter what

 
Reply to this topicStart new topic

If Statement triggering no matter what

redcometix
5 Feb, 2008 - 02:38 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 4

CODE
// Check Writer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


class Numbers
{
private:
    int number;
    int hundrednumber;
    int tensnumber;
    int onesnumber;

public:
    void setThous(int);
    int getThous();
    void setHundred(int);
    int getHundred();
    void setTens(int);
    int getTens();
    void setOnes(int);
    int getOnes();
};
void Numbers::setThous(int n)
{
    number = n/1000;
}
int Numbers::getThous()
{
    return number;
}
void Numbers::setHundred(int n)
{
    hundrednumber = (n - (number*1000))/100;
}
int Numbers::getHundred()
{
    return hundrednumber;
}

void Numbers::setTens(int n)
{
    tensnumber = (n - (number*1000)-(hundrednumber*100))/10;
}
int Numbers::getTens()
{
    return tensnumber;
}
void Numbers::setOnes(int n)
{
    onesnumber = (n - (number*1000)-(hundrednumber*100)-(tensnumber*10));
}
int Numbers::getOnes()
{
    return onesnumber;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int switchnumber2 = 5555;
    Numbers brady;
    int NumberEntry;
    cout << "Please input a Number 0-9999\n";
    cin >> NumberEntry;
    brady.setThous(NumberEntry);
    //cout << brady.getThous();
    int switchnumber = brady.getThous();
    switch (switchnumber)
    {
    case 9:
        cout << "Nine Thousand ";
        break;
    case 8:
        cout << "Eight Thousand ";
        break;
    case 7:
        cout << "Seven Thousand ";
        break;
    case 6:
        cout << "Six Thousand ";
        break;
    case 5:
        cout << "Five Thousand ";
        break;
    case 4:
        cout << "Four Thousand ";
        break;
    case 3:
        cout << "Three Thousand ";
        break;
    case 2:
        cout << "Two Thousand " ;
        break;
    case 1:
        cout << "One Thousand ";
        break;
    case 0:
        break;
    }
    brady.setHundred(NumberEntry);
    switchnumber = brady.getHundred();
switch (switchnumber)
    {
    case 9:
        cout << "Nine Hundred ";
        break;
    case 8:
        cout << "Eight Hundred ";
        break;
    case 7:
        cout << "Seven Hundred ";
        break;
    case 6:
        cout << "Six Hundred ";
        break;
    case 5:
        cout << "Five Hundred ";
        break;
    case 4:
        cout << "Four Hundred ";
        break;
    case 3:
        cout << "Three Hundred ";
        break;
    case 2:
        cout << "Two Hundred ";
        break;
    case 1:
        cout << "One Hundred ";
        break;
    case 0:
        break;
    }
brady.setTens(NumberEntry);
switchnumber = brady.getTens();
//cout << switchnumber;
switch (switchnumber)
    {
    case 9:
        cout << "Ninety ";
        break;
    case 8:
        cout << "Eighty ";
        break;
    case 7:
        cout << "Seventy ";
        break;
    case 6:
        cout << "Sixty ";
        break;
    case 5:
        cout << "Fifty ";
        break;
    case 4:
        cout << "Fourty ";
        break;
    case 3:
        cout << "Thirty ";
        break;
    case 2:
        cout << "Twenty ";
        break;
    case 1:
        brady.setOnes(NumberEntry);
        switchnumber2 = brady.getOnes();
        switch (switchnumber2)
            {
                case 9:
                    cout << "Nineteen Dollars " << endl;
                    break;
                case 8:
                    cout << "Eighteen  Dollars" << endl;
                    break;
                case 7:
                    cout << "Seventeen Dollars " << endl;
                    break;
                case 6:
                    cout << "Sixteen  Dollars " << endl;
                    break;
                case 5:
                    cout << "Fifteen Dollars " << endl;
                    break;
                case 4:
                    cout << "Fourteen Dollars " << endl;
                    break;
                case 3:
                    cout << "Thirteen Dollars " << endl;
                    break;
                case 2:
                    cout << "Twelve Dollars " << endl;
                    break;
                case 1:
                    cout << "Eleven Dollars " << endl;
                    break;
                case 0:
                    cout << "Ten Dollars " << endl;
                    break;
                }
        break;
    case 0:
        break;
    }
if (switchnumber2 = 5555)
{
brady.setOnes(NumberEntry);
switchnumber2 = brady.getOnes();
switch(switchnumber2)
{
case 1:
    cout << "One Dollars." << endl;
    break;
case 2:
    cout << "Two Dollars." << endl;
    break;
case 3:
    cout << "Three Dollars." << endl;
    break;
case 4:
    cout << "Four Dollars." << endl;
    break;
case 5:
    cout << "Five Dollars." << endl;
    break;
case 6:
    cout << "Six Dollars." << endl;
    break;
case 7:
    cout << "Seven Dollars." << endl;
    break;
case 8:
    cout << "Eight Dollars." << endl;
    break;
case 9:
    cout << "Nine Dollars." << endl;
    break;
case 0:
    cout << "Dollars." << endl;
    break;
}
}
else
{}
    return 0;
}



We're supposed to be writing a program that will take a 4 digit number and write it out in English. I'm able to get the program to work, except for when the number is inbetween 10-19. I initialized switchnumber2 with a large number so that if it was untouched when the program ran, it would do that last switch, otherwise it would just end. However regardless of if switchnumber2 is 5 or 5555, it still goes through that last if statement. Why, and how can I correct said problem?


Here's what it looks like when it's working incorrectly:
IPB Image

Here's what it's supposed to look like:
IPB Image
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: If Statement Triggering No Matter What
5 Feb, 2008 - 03:18 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,113



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You don't have a default in your case statements. I'm not sure if this is causing your error, but you should always have a fallout of the switch.
User is offlineProfile CardPM
+Quote Post

redcometix
RE: If Statement Triggering No Matter What
5 Feb, 2008 - 03:53 PM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 4

It didn't help, but thanks for reminding me about that.
User is offlineProfile CardPM
+Quote Post

KYA
RE: If Statement Triggering No Matter What
5 Feb, 2008 - 04:12 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,847



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(redcometix @ 5 Feb, 2008 - 05:53 PM) *

It didn't help, but thanks for reminding me about that.



Here is your problem:

CODE

if (someNumber = 5555)

////////bla bla bla


That's the assignment operator, not the equals operator which is ==


Try that instead of =

--KYA
User is online!Profile CardPM
+Quote Post

redcometix
RE: If Statement Triggering No Matter What
5 Feb, 2008 - 04:15 PM
Post #5

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 4

Worked like a charm. Thanks. Not sure why I didn't catch that when reading over my code...
User is offlineProfile CardPM
+Quote Post

KYA
RE: If Statement Triggering No Matter What
5 Feb, 2008 - 04:17 PM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,847



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(redcometix @ 5 Feb, 2008 - 06:15 PM) *

Worked like a charm. Thanks. Not sure why I didn't catch that when reading over my code...


My brain always reads the two as the same sometimes. Especially if you're just glancing over it. Glad it worked out for you.

--KYA
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:15AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month