Do While Looping Help

This is a discussion on Do While Looping Help within the C++ Programming forums, part of the General Programming Boards category; I'm writing a simple program for my chemistry class that does ideal gas law conversions. I have successfully gotten the ...

  1. #1
    Niq
    Niq is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    Do While Looping Help

    I'm writing a simple program for my chemistry class that does ideal gas law conversions. I have successfully gotten the program to do the conversions and calculations but I cannot get it to loop. I would like it to ask the user, "Do you need to perform another conversion?". If the user answers yes, it loops, if the answer is no, it stops. I'm pretty new to all this (surprise, surprise eh?) so bare with me. Here is the code I have thus far. Any help would be greatly appreciated!

    Code:
    char yes;
    
    int main()
    
    { 
    
     do
    
     { 
    
      float pressure;
    
      cout<<"Define P: ";
    
      cin>>pressure;
    
        float volume;
    
        cout<<"Define V: ";
    
        cin>>volume;
    
          float nofmoles;
    
          cout<<"Define n: ";
    
          cin>>nofmoles;
    
            float temperature;
    
    	cout<<"Define T: ";
    
    	cin>>temperature;
    
      if(pressure==0)
    
      cout<<"Pressure equals "<<(nofmoles*0.08206*temperature)/volume<<" atms.";
    
      if(volume==0)
    
      cout<<"Volume equals "<<(nofmoles*0.08206*temperature)/pressure<<" liters.";
    
      if(nofmoles==0)
    
      cout<<"Number of moles equals "<<(pressure*volume)/(0.08206*temperature)<<".";
    
      if(temperature==0)
    
      cout<<"Temperature equals "<<(pressure*volume)/(0.08206*nofmoles)<<" degrees K.";
    
        cout<<"
    ";
    
        cout<<"Do you need to perform another conversion? ";
    
        cin>>question;
    
     }while(question==yes);
    
      return 0;
    
    }
    Thanks in advance!

  2. #2
    Niq
    Niq is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Before any replies pop up telling me this forum is not for homework help, I am writing this program on my own accord and is in no way homework. I just figured this would be a simple program to learn with as it includes basic C++ code.

    Thanks again

  3. #3
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    The way you have it is, char question;. This is fine, but a single char only holds one character.

    So you have two options, have the user enter either 'Y' or 'N' like:
    Code:
    char question;
    //  ...
    cout << "Go again?(Y/N): ";
    cin >> question;
    }
    while(question == 'Y' || question == 'y');
    Or, you can have a character array:
    Code:
    char question[5];
    // ...
    
    cout << "Enter yes to do it again: ";
    cin.getline(question,5);
    }while(strcmp("Yes",question));
    You need the cstring header file for strcmp, and you use the getline() member function of cin for char arrays, that way you read the amount of characters you want.

    Also you need to have 1 extra element in your array for a null terminator.

    Hope that helps.
    If you ever need a hug, just ask.

  4. #4
    Niq
    Niq is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Worked like a charm CheesyMoo, thanks for the quick help!

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    just to mention, we're willing to help with homework, but we won't write the program for anyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 02:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM

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