Click here to Skip to main content
15,399,959 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:

<pre>#include <iostream>
#include <cmath>
#include "jsdbejbfkejfb.h"
#include <regex>

void idkwwt item: ";
    std::cin >> input_string;
    valid_input = is_integer(input_string);
    // if input is not an integer, print an error message
    if (valid_input == false) {
      std::cout << "Enter an integer!\n";
    } else {  // if it is an int, check whether in range
      input = std::stoi(input_string);  // convert to int
      if (input >= 1 && input <= menu_items) {
        valid_input = true;
      } else {
        std::cout w
void select_menu_item(int input) {
  switch (input) {
    case 1:
    case 2:

void print_main_menu() {

void goa
  std::cout << "\n>> Current Divider Calculator\n";
  std::cout << "\nSome code here does something useful\n";

void menu_item_3() {
  std::cout << "\n>&gs

#ifndef Voltage_H
float _Vs;
float _Vout;




#include "Voltage.h"


Voltage::get_Vout ()
  _Vout = (_Vs * _R2) / (_R1 + _R2);
  return _Vout;

What I have tried:

Ive tried repeating the calculation with different numbers but its the same output so im guess it is not doing the calculation
Updated 8-Jan-22 5:17am
k5054 7-Dec-21 17:18pm
I'll give you a clue to help with the debuging: If you put a break point at Voltage::voltage_divider_Calc(), what are the values of the voltage member variabes _R1, _R2, and _Vs? Why?
k5054 7-Dec-21 17:37pm
Oops, you're right (there probably should be, though hint hint). I guess I meant Voltage::get_Vout()

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   return value * value;

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary.
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
I see two problems with your code, one of which you have asked about. In voltage_divider_Calc you have a voltage object and you call its get_Vout method BUT you did not set its members so it can not give you a valid result.

The other problem is if you select return to the main menu you call the function go_back_to_main_menu which essentially restarts the menu. The problem with that is you have introduced recursion into your program. It will continue until exit is selected and then you call exit(). That is not recommended practice at all. You need to make your main menu a top level function that loops continuously until you want to exit out of the program because that will give you an opportunity to release any resources you have allocated. Calling exit does allow that.
Member 15459232 7-Dec-21 17:28pm
what do you mean by you did not set its members can you clarify please
Rick York 7-Dec-21 18:08pm
Look at the implementation of voltage_divider_Calc(). It acquires input for three local variables and then it does nothing with those values. The voltage object has its get_Vout() method called but the object's members are not set to the values that were input.
Look at your code:
You create a Voltage object, but at that point none of its members have any values. You then call cin to get values for the local variables _R1, _R2 and _Vs. You then call the get_Vout member function of your Voltage object, to calculate something. But since that object has not been initialised with any values it has nothing to work with so the answer will most likely be zero, or some random number.
Voltage voltage;
std::cout << "\n Enter a value for top resistor R1\n";
std::cin >>  (_R1);
std::cout << "\n Enter a value for bottom resistor R2\n";
std::cin >>  (_R2);
std::cout << "\n Enter a value for the source voltage Vs\n";
std::cin >>  (_Vs);
float Vout = voltage.get_Vout();

You need to create the Voltage object after you have got the input values. You then need to call the setter methods (set_R1 etc.) to set those values into the object before you make the call to get_Vout.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900