If statement being ignored

This is a discussion on If statement being ignored within the C++ Programming forums, part of the General Programming Boards category; hi i am wondering if any of you guys can help me find the problem with my code. When i ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    If statement being ignored

    hi

    i am wondering if any of you guys can help me find the problem with my code. When i run it there are no errors but this if statement isn't working at all

    Code:
     //this is where the amount of time on the call is calculated and where the countdown begins
        if (type_cost==1)
        {
            for(count=sum_coins; count=0; count--)
            {
                cout<<count;
                Sleep(600);
            }
        }
        else if (type_cost==2)
        {
            for(count=sum_coins*2; count=0; count--)
            {
                cout<<count;
                Sleep(600);
            }
        }
        else if (type_cost==3)
        {
            for(count=sum_coins/4; count=0; count--)
            {
                cout<<count;
                Sleep(600);
            }
        }
        else;
        
        cout<<"Your Time is up"<<endl;
    system("pause");
    return 0;
    }
     //this is the end of my code
    and so i am wondering if someone can tell me what i am doing wrong full code below

    Code:
    #include <windows.h>
    #include <iostream.h>
    
    char call_type;
    int type_cost;
    int coins;
    int accepted_coins;
    int sum_coins;
    int count;
    int phone_num;
    
    int main()
    {
        system("pause");
        //The operning menu
        cout<<"========== Telephone kiosk Menu =========="<<"\n\n"<<"L  Local"<<"\n";
        cout<<"T  Trunk"<<"\n"<<"I  International"<<"\n"<<"E - Exit"<<"\n"<<"======================================"<<endl;
        cin>>call_type;
        //This is where each call type is assigned a numerical value
        switch (call_type)
        {
            case 'T':
            case 't':   
                cout<<"You have selected a Trunk Call"<<endl;
                type_cost=1;
                break;
            case 'L':
            case 'l': 
                cout<<"You have selected a Local Call"<<endl;
                type_cost=2;
                break;
            case 'I':
            case 'i':    
                cout<<"You have selected an International Call"<<endl;
                type_cost=3;
                break;
            case 'E':
            case 'e':
                cout<<"You have chosen to Exit \n Goodbye"<<endl;
                Sleep(1000);
                return 0;
                break;
            default:
                cout<<"Please select one of the following"<<endl<<"========== Telephone kiosk Menu =========="<<"\n\n"<<"L  Local"<<"\n";
                cout<<"T  Trunk"<<"\n"<<"I  International"<<"\n"<<"E - Exit"<<"\n"<<"======================================"<<endl;
        }
        
        //This is the confirmation of the call type        
        cout<<"please insert your coins now ...\n";
        cout<<"You have requested ";
        if (type_cost==1)
            cout<<"a Trunk Call \n";
        else  if(type_cost==2)
            cout<<"a Local Call \n";
        else  if(type_cost==3)
            cout<<"an International Call  \n";
        cout<<"The charge for this type of call is ";        
        if (type_cost==1)
            cout<<"10p for 10 seconds \n";
        else if (type_cost==2)
            cout<<"10p for 20 seconds \n";
        else if (type_cost==3)
            cout<<"10p for 4 seconds  \n";
         //This is the validation and addition of the coins entered   
        cout<<"Please only enter on 5p 10p 20p 50p or 100p coins\n\n";
        cout<<"Press 0 why you have finished entering coins\n";
        cin>>coins;
            if ((coins==5)||(coins==10)||(coins==20)||(coins==50)||(coins==100))
            {
                sum_coins=sum_coins+coins;
                do
                {
                    
                    cout<<"Please enter the next coin\n";
                    cin>>coins;
                    if ((coins==5)||(coins==10)||(coins==20)||(coins==50)||(coins==100))
                    {
                        sum_coins=sum_coins+coins;
                        cout<<"Your total is "<<sum_coins<<endl;
                    }
                    else if (coins==0);
                                    
                    else
                    {
                        cout<<"Please only enter on 5p 10p 20p 50p or 100p coins\n";
                        cin>>coins;
                    }
                }while(coins !=0);
            }
            else
            cout<<"Please only enter on 5p 10p 20p 50p or 100p coins\n";
        system("cls");
        //this is where the user enters the number they with to dial and the dialing begins
        cout<<"Please enter the number you wish to dial\n";
        cin>>phone_num;
        system("cls");
        cout<<"Now dialing "<<phone_num<<endl;
        Sleep(1000);
        cout<<"Connected - Please start your conversation\n";
        //this is where the amount of time on the call is calculated and where the countdown begins
        if (type_cost==1)
        {
            for(count=sum_coins; count=0; count--)
            {
                cout<<count;
                Sleep(600);
            }
        }
        else if (type_cost==2)
        {
            for(count=sum_coins*2; count=0; count--)
            {
                cout<<count;
                Sleep(600);
            }
        }
        else if (type_cost==3)
        {
            for(count=sum_coins/4; count=0; count--)
            {
                cout<<count;
                Sleep(600);
            }
        }
        else;
        
        cout<<"Your Time is up"<<endl;
    system("pause");
    return 0;
    }
     //this is the end of my code

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    Code:
    for(count=sum_coins; count=0; count--)
    The for loop looks to be the one that does not do anything.


    The code here is likely to be wrong.
    Code:
    count=0
    This is what you most likely want. Notice the double equal sign for comparison NOT assignment.

    Code:
    count == 0
    Or maybe this

    Code:
    count > 0
    Tim S.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    thank you very much i changed the code to
    Code:
     count>=0
    and now the whole program works perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why won't this simple statement work?
    By nick753 in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2010, 11:20 PM
  2. bizarre print statement glitch
    By spongefreddie in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 10:39 AM
  3. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  4. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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