Thread: Not Going Through If Statements

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Not Going Through If Statements

    Ok, so this is a simple metric conversion program, except when I try to print out the two variable's, of when you choose what your converting from and what your converting to. Kind of hard to explain, but for some reason it's not passing through the if statement's, it seems that it should, but I can't seem to pinpoint why it isn't.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using std::string;
    using namespace std;
    
    int main()
    {
     cout<<"This Is A Metric Conversion Program, Use It Wisely."<<endl<<endl<<endl;
    
      char convertfrom [10];
      char convertfrom2 [10];
      char convertto [10];
      char convertto2 [10];
    
    
       cout<<"Choose The Number Of The Weight Type You Wish To Convert FROM:"<<endl;
       cout<<"1.Centigram"<<endl;
       cout<<"2.Decigram"<<endl;
       cout<<"3.Dekagram"<<endl;
       cout<<"4.Gram"<<endl;
       cout<<"5.Hectogram"<<endl;
       cout<<"6.Kilogram"<<endl;
       cout<<"7.Metric Ton"<<endl;
       cout<<"8.Milligram"<<endl;
       cout<<"9.Microgram"<<endl;
       cout<<"10.Metric Pound"<<endl;
        cin>>convertfrom;
         cin.ignore();
    
       cout<<"Now Choose The Number Of The Weight Type You Wish To Convert TO:"<<endl;
       cout<<"1.Centigram"<<endl;
       cout<<"2.Decigram"<<endl;
       cout<<"3.Dekagram"<<endl;
       cout<<"4.Gram"<<endl;
       cout<<"5.Hectogram"<<endl;
       cout<<"6.Kilogram"<<endl;
       cout<<"7.Metric Ton"<<endl;
       cout<<"8.Milligram"<<endl;
       cout<<"9.Microgram"<<endl;
       cout<<"10.Metric Pound"<<endl;
        cin>>convertto;
         cin.ignore();
    
         cout<<convertfrom<<endl<<convertto;
    
    
    
    
    
     if(convertfrom=="1")
      {
       convertfrom2=="Centigram";
      }
    
     if(convertfrom=="2")
      {
       convertfrom2=="Decigram";
      }
    
     if(convertfrom=="3")
      {
       convertfrom2=="Dekagram";
      }
    
     if(convertfrom=="4")
      {
       convertfrom2=="Gram";
      }
    
     if(convertfrom=="5")
      {
       convertfrom2=="Hectogram";
      }
    
     if(convertfrom=="6")
      {
       convertfrom2=="Kilogram";
      }
    
     if(convertfrom=="7")
      {
       convertfrom2=="Metric Ton";
      }
    
     if(convertfrom=="8")
      {
       convertfrom2=="Milligram";
      }
    
     if(convertfrom=="9")
      {
       convertfrom2=="Microgram";
      }
    
     if(convertfrom=="10")
      {
       convertfrom2=="Metric Pound";
      }
    
    
     if(convertto=="1")
      {
       convertto2=="Centigram";
      }
    
     if(convertto=="2")
      {
       convertto2=="Decigram";
      }
    
     if(convertto=="3")
      {
       convertto2=="Dekagram";
      }
    
     if(convertto=="4")
      {
       convertto2=="Gram";
      }
    
     if(convertto=="5")
      {                
       convertto2=="Hectogram";
      }
    
     if(convertto=="6")
      {
       convertto2=="Kilogram";
      }
    
     if(convertto=="7")
      {
       convertto2=="Metric Ton";
      }
    
     if(convertto=="8")
      {
       convertto2=="Milligram";
      }
    
     if(convertto=="9")
      {
       convertto2=="Microgram";
      }
    
     if(convertto=="10")
      {
       convertto2=="Metric Pound";
      }
    
    cout<<convertfrom2<<endl<<convertto2;
    
    cin.get();
    return 1;
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    if(strcmp(convertto, "somenumberhere") == 0)
    Yah.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You can't compare character arrays with ==. Either use a string object from the <string> library or use strcmp() from the <cstring> library.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    In the body of your if statements, you're using the wrong equality operator.

    == means comparison, it returns true if the two operands are equal and false otherwise
    = means assignment, it sets the left hand operand equal to the right hand operand.

    You need to replace all the lines that say convertfrom=="whatever" to convertfrom="whatever"

    Also, it's pointless to use arrays here since you're only expecting one character as input. Just use characters for input instead of arrays. It's not really that important, but you're wasting memory by using a 10 character array for a one character input. If you take my advice to use characters, change all your if statements to use single quotes around the numbers instead of double quotes.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Actually, don't use '=' on convertfrom2 (which I'm sure is what maxhavoc was referring to). Use

    Code:
    strcpy(converfrom2, "massunithere");
    You could probably make this easier and use std::string's, seing as they're already included in your project.

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Wouldn't it be easier to use one big switch statement rather than a bunch of if ones?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Me
    Join Date
    Jul 2006
    Posts
    71
    Ok, thanks that worked, i'm just used to basic where the language is smart enough to figure out if it's comparing two strings or not.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yep, cause basic is so smart that no matter what syntax you use, it can figure out what you want to do. You pin pointed it, C++ is just some dumb language that requires you to use standard syntax and basic is so super advanced. Good call.

    Why don't you try using strcmp in basic and see what happens... or maybe just type "Compar des 2 strings plz!!" and maybe it will figure it out. If it's too difficult for you to memorize some simple standard syntax then perhaps you should avoid the programming field completely or just stick to the language you already know.
    Sent from my iPadŽ

  9. #9
    Me
    Join Date
    Jul 2006
    Posts
    71
    I was joking...and no this is not too difficult, memorize? I've just started programming C++, you have to know something to memorize it...

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by relyt_123
    I've just started programming C++, you have to know something to memorize it...
    Exactly. If you don't know the syntax, then why insist that it's the languages fault for not knowing your syntax? You don't look clever or funny when you blame something that isn't at fault, whether it was meant to be a joke or not... and in the end your joking habit will cause you to mix up the syntax you feel it should be with the syntax that really is.

    I'm not trying to get down on you, I'm just making sure you get that in programming, you play by the languages rules. The programmer is supposed to be the smart one, not the computer.
    Sent from my iPadŽ

  11. #11
    Me
    Join Date
    Jul 2006
    Posts
    71

    Agreed.

    Agreed, sorry for any misunderstanding, I got my program working by the way. But sorry about that.

  12. #12
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    The programmer is supposed to be the smart one, not the computer.
    Yeah, I found out the hard way that the computer is only going to do what you tell it to do... Even if you don't know what you are telling it to do.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What is this program supposed to do - convert, say, input in grams to output in tons, or any other units chosen by the user. In the code, I only see you trying to modify strings (hint std::string is smarter than char arrays), but not doing anything useful.

    Can you write this program in Basic? (Hint: no goto-s ) Then re-writing it in C++ shouldn't be too hard.

    (If you want to do any convertions, you could choose a base unit and use the powers of 10.)

  14. #14
    Me
    Join Date
    Jul 2006
    Posts
    71

    Rewrote it

    I just decided to re-write it anyway, using a two-dimensional array for the calculations.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <cstring>
    
    using std::string;
    using namespace std;
    
    int main()
    {
    
    system("title Metric Converter");
    
     cout<<"Simple Metric Conversion Program\nWritten By:Relyt_123"<<endl<<endl<<endl;
    
      int convertfrom;
      int convertto;
      string arrayone [11];
      double arraytwo [11][11];
      int number;
      double answer;
    
    
      arrayone[1]="Metric Ton";
      arrayone[2]="Kilogram";
      arrayone[3]="Metric Pound";
      arrayone[4]="Hectogram";
      arrayone[5]="Dekagram";
      arrayone[6]="Gram";
      arrayone[7]="Decigram";
      arrayone[8]="Centigram";
      arrayone[9]="Milligram";
      arrayone[10]="Microgram";
    
    arraytwo[1][1]=1;
     arraytwo[1][2]=0.001;
     arraytwo[1][3]=0.0005;
     arraytwo[1][4]=0.0001;
     arraytwo[1][5]=0.00001;
     arraytwo[1][6]=0.000001;
     arraytwo[1][7]=0.0000001;
     arraytwo[1][8]=0.00000001;
     arraytwo[1][9]=0.000000001;
     arraytwo[1][10]=0.00000000001;
    arraytwo[2][1]=1000;
     arraytwo[2][2]=1;
     arraytwo[2][3]=0.5;
     arraytwo[2][4]=0.1;
     arraytwo[2][5]=0.01;
     arraytwo[2][6]=0.0001;
     arraytwo[2][7]=0.00001;
     arraytwo[2][8]=0.000001;
     arraytwo[2][9]=0.0000001;
     arraytwo[2][10]=0.0000000001;
    arraytwo[3][1]=2000;
     arraytwo[3][2]=2;
     arraytwo[3][3]=1;
     arraytwo[3][4]=0.2;
     arraytwo[3][5]=0.02;
     arraytwo[3][6]=0.001;
     arraytwo[3][7]=0.0001;
     arraytwo[3][8]=0.00001;
     arraytwo[3][9]=0.000001;
     arraytwo[3][10]=0.000000001;
    arraytwo[4][1]=10000;
     arraytwo[4][2]=10;
     arraytwo[4][3]=5;
     arraytwo[4][4]=1;
     arraytwo[4][5]=0.1;
     arraytwo[4][6]=0.01;
     arraytwo[4][7]=0.001;
     arraytwo[4][8]=0.0001;
     arraytwo[4][9]=0.00001;
     arraytwo[4][10]=0.00000001;
    arraytwo[5][1]=100000;
     arraytwo[5][2]=100;
     arraytwo[5][3]=50;
     arraytwo[5][4]=10;
     arraytwo[5][5]=1;
     arraytwo[5][6]=0.1;
     arraytwo[5][7]=0.01;
     arraytwo[5][8]=0.001;
     arraytwo[5][9]=0.0001;
     arraytwo[5][10]=0.0000001;
    arraytwo[6][1]=1000000;
     arraytwo[6][2]=1000;
     arraytwo[6][3]=500;
     arraytwo[6][4]=100;
     arraytwo[6][5]=10;
     arraytwo[6][6]=1;
     arraytwo[6][7]=0.1;
     arraytwo[6][8]=0.01;
     arraytwo[6][9]=0.001;
     arraytwo[6][10]=0.000001;
    arraytwo[7][1]=10000000;
     arraytwo[7][2]=10000;
     arraytwo[7][3]=5000;
     arraytwo[7][4]=1000;
     arraytwo[7][5]=100;
     arraytwo[7][6]=10;
     arraytwo[7][7]=1;
     arraytwo[7][8]=0.1;
     arraytwo[7][9]=0.01;
     arraytwo[7][10]=0.00001;
    arraytwo[8][1]=100000000;
     arraytwo[8][2]=100000;
     arraytwo[8][3]=50000;
     arraytwo[8][4]=10000;
     arraytwo[8][5]=1000;
     arraytwo[8][6]=100;
     arraytwo[8][7]=10;
     arraytwo[8][8]=1;
     arraytwo[8][9]=0.1;
     arraytwo[8][10]=0.0001;
    arraytwo[9][1]=1000000000;
     arraytwo[9][2]=1000000;
     arraytwo[9][3]=500000;
     arraytwo[9][4]=100000;
     arraytwo[9][5]=10000;
     arraytwo[9][6]=1000;
     arraytwo[9][7]=100;
     arraytwo[9][8]=10;
     arraytwo[9][9]=1;
     arraytwo[9][10]=0.001;
    arraytwo[10][1]=1E+12;
     arraytwo[10][2]=1000000000;
     arraytwo[10][3]=500000000;
     arraytwo[10][4]=100000000;
     arraytwo[10][5]=10000000;
     arraytwo[10][6]=1000000;
     arraytwo[10][7]=100000;
     arraytwo[10][8]=10000;
     arraytwo[10][9]=1000;
     arraytwo[10][10]=1;
    
      cout<<"Choose The Number Of The Weight Type You Wish To Convert From:"<<endl;
       for(int i=1;i<11;i++){
        cout<<i<<". "<<arrayone[i]<<endl;
         }
          cout<<"\n";
           cin>>convertfrom;
           cin.ignore();
    
     cout<<"\nWhat Number Do You Wish To Convert?\n"<<endl;
      cin>>number;
      cin.ignore();
    
      cout<<"\nNow Choose The Number Of The Weight Type You Wish To Convert This Number To:\n"<<endl;
       for(int i=1;i<11;i++)
        {
         cout<<i<<". "<<arrayone[i]<<endl;
        }
        cout<<"\n";
        cin>>convertto;
        cin.ignore();
         cout<<"\n";
    
         if(convertfrom>10 || convertfrom<1)
         {
          return 0;
         }
         if(convertto>10 || convertto<1)
         {
          return 0;
         }
    
    
         answer=number*arraytwo[convertfrom][convertto];
    
    cout<<number<<" "<<arrayone[convertfrom]<<"/s Equal/s "<<answer<<" "<<arrayone[convertto]<<"/s."<<endl;
    
    cin.get();
    return 1;
    }
    Last edited by relyt_123; 07-26-2006 at 10:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM