Can someone point me in the right direction?

This is a discussion on Can someone point me in the right direction? within the C++ Programming forums, part of the General Programming Boards category; Hello, I am working on a project for my C++ class, I have nearly completed the project, and the code ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Can someone point me in the right direction?

    Hello, I am working on a project for my C++ class, I have nearly completed the project, and the code more or less works. However, I am getting hung up on one aspect. The project is small, its to write a program that calculates the equivalent resistance of "n" parallel resistors. The code I have written below works fine until "0" is inputed to calculate a new set.

    Code:
    # include <iostream.h>
    
    
    int main(void) {
    
      double Req, Resistor, firstResistor;
      cout.setf(ios::showpoint | ios::fixed);
      cout.precision(4);
    
    
      do {
          int n = 1;
    
                         do {
                         cout << endl;
                         cout << "Please input resistor-" <<n<< ", or 0 to quit: ";
    
                                            if (n == 1) {
                                            cin >> firstResistor;
    
                                                 if (firstResistor < 0 ) {
                                                 cout << "Error: Resistance must be positive";
                                                 cout << endl;       //negative action//
                                                 continue;
                                                 }
    
                                                 if (firstResistor > 0) {
                                                 Req=firstResistor;
                                                 }
    
                                                 if (firstResistor ==  0) {                           //exit statement
                                                 return 0;
                                                 }
    
                                            }
    
    
                                            else if (n > 1) {
                                            cin >> Resistor;
    
                                                if (Resistor < 0 ) {
                                                cout << "Error: Resistance must be positive";
                                                cout << endl;
                                                continue;                                                  //negative action//
                                                           }
    
                                                if (Resistor ==  0) {
                                                break;
                                                }                                                       //exit statement
    
                                                if (Resistor > 0) {
                                                Req = ( 1. /(( 1./ Req )+( 1./ Resistor )));
                                                }
    
                                            }
    
    
    
    
                            n++;
                            } while ((firstResistor != 0) && (Resistor != 0));
    
    
    
         cout << endl;
         cout << "The equivalent resistance is : " <<Req<< " Ohms";
         cout << endl;
    
    
    
    
         }while ((firstResistor == 0) || (Resistor == 0));
         return 0;
    
    }

    The output looks like the attached picture below.

    I'm not sure why the program is showing the output after completing the first "if (n=1)" statement instead of looping again and going on to the "else if (n>1)." As you can see, it doesnt do it for the first set of resistances entered.

    I have been toying with this for several hours, and still have not gotten a solution. I am a first time coder, and this is the first project I have coded completely from start to finish. Much of this might as well be greek to me, but I am trying to learn.

    I am not asking for anyone to write this for me, I just need someone to shed some light on what I am doing wrong.

    Thank you very much in advance.

    EDIT: I think the problem is caused by the value of "resistor" still being zero from the first run, thus the program is only calculating the resistance of the first resistor "firstResistor" (which was changed to Req) Could this be the problem?
    Attached Images Attached Images  
    Last edited by MadMac; 09-17-2005 at 04:00 PM.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Ok, I this is what I did to rectify the problem. I defined firstResistor and Resistor to be a numerical value in the outer loop, figuring that they would be redefined in the inner loops before they had any effect on the output. Is this an acceptable fix?

    Code:
    # include <iostream.h>
    
    
    int main(void) {
    
      double Req, Resistor, firstResistor;
      cout.setf(ios::showpoint | ios::fixed);
      cout.precision(4);
    
    
      do {
          int n = 1;
          firstResistor=1.;
          Resistor=1.;
                         do {
                         cout << endl;
                         cout << "Please input resistor-" <<n<< ", or 0 to quit: ";
    
                                            if (n == 1) {
                                            cin >> firstResistor;
    
                                                 if (firstResistor < 0 ) {
                                                 cout << "Error: Resistance must be positive";
                                                 cout << endl;       //negative action//
                                                 continue;
                                                 }
    
                                                 if (firstResistor > 0) {
                                                 Req=firstResistor;
                                                 }
    
                                                 if (firstResistor ==  0) {                           //exit statement
                                                 return 0;
                                                 }
    
                                            }
    
    
                                            else if (n > 1) {
                                            cin >> Resistor;
    
                                                if (Resistor < 0 ) {
                                                cout << "Error: Resistance must be positive";
                                                cout << endl;
                                                continue;                                                  //negative action//
                                                           }
    
                                                if (Resistor ==  0) {
                                                break;
                                                }                                                       //exit statement
    
                                                if (Resistor > 0) {
                                                Req = ( 1. /(( 1./ Req )+( 1./ Resistor )));
                                                }
    
                                            }
    
    
    
    
                            n++;
                            } while ((firstResistor != 0) && (Resistor != 0));
    
    
    
         cout << endl;
         cout << "The equivalent resistance is : " <<Req<< " Ohms";
         cout << endl;
    
    
    
    
         }while ((firstResistor == 0) || (Resistor == 0));
         return 0;
    
    }

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    I don't think that does anything (your "fix", I mean).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Quote Originally Posted by dwks
    I don't think that does anything (your "fix", I mean).

    It worked, though. I had to have the variables initialized, otherwise the value of 0 carried over from the previous run. The the boolean statements told the program to go and calculate the equivalent resistance when any inputed resistance value equals zero. Since the variable "Resistance" was still zero, the program wasnt running the second part of the inner loop, just printing the output of the first.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    iostream.h is not a C++ header. Please get a book on C++ so you may learn it properly.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Please get a book on C++ so you may learn it properly.
    Most books aren't recent enough to include this change in the standard.

    What you should do, is drop the '.h' extension from standard header files. If the file is also part of the C standard library, you precede it with a 'c'.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    becomes

    Code:
    #include <iostream> // standard C++
    #include <cstdlib> // standard C
    #include <windows.h> // non-standard
    You also need to use namespaces. The simplest way to do this until you start making your own namespaces, etc... is to simply add the line, "using namespace std;" after your includes. This has some drawbacks, but is the best method until you start using other namespaces.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Quote Originally Posted by orbitz
    iostream.h is not a C++ header. Please get a book on C++ so you may learn it properly.
    This is how my psuedo code that was given to us started it... there was no need for your abrasive tone.

    Thank you to everyone else for contructive help. I have corrected this error. This is my first program I've coded, and its early in the class. Thus far, we have not been changing anything that the compiler gives us (or that was given to us in a text file by the instructor).
    Last edited by MadMac; 09-18-2005 at 11:00 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    #include <iostream> // standard C++
    #include <cstdlib> // standard C
    #include <windows.h> // non-standard
    Well, actually, <cstdlib> is the C++ standard name for the C header <stdlib.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Point in the right direction
    By peanutym in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2008, 08:49 PM
  2. Point me in the right direction
    By vampje in forum C++ Programming
    Replies: 0
    Last Post: 06-07-2006, 03:52 AM
  3. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 08:26 AM
  4. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 11:59 AM
  5. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM

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