Question

Add a menu to the existing code already written to contain the following options Change Input...

Add a menu to the existing code already written to contain the following options

Change Input voltage

Add a single resistor

Delete resistor

Edit resistor

Group add a series of resistors

Display network

Quit program

Each item in the menu will probably end up being a function. A do-while loop and switch/case statement is probably best way to implement a menu.

You need to create at least 4 functions.

Existing code

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// You must use this structure
struct node {

string name;
double resistance;
double voltage_across;
double power_across;

};

// An Example of the type of function you may want to create. It take node name
string nodeName() {
string node_name;
cout << "Enter Node Name: ";
cin >> node_name;

return node_name;
}

// This function takes voltage from user
double getVolt()
{
double source_voltage;
cout << "Enter Voltage: ";
cin >> source_voltage;
return source_voltage;
}

void calcVolt(vector<node>& N, double curr)
{
unsigned int i;
for (i = 0; i < N.size() ; ++i)
{
N[i].voltage_across = curr * N[i].resistance ;
}
}

// This function takes resistance from user
double getResistance(vector<node>& N)
{
double resistance , i = 0, sum = 0;
while (1)
{
cout << "Enter Resistance (0 to exit): ";
cin >> resistance;
if (resistance != 0)
{
N.push_back(node());
N[i].name = "Node ";
N[i].resistance = resistance;
sum += resistance;
}
else
return sum;

i++;
}
}

// This function calculates power
void calcPower(vector<node>& N, double curr)
{
unsigned int i;
for (i = 0; i < N.size() ; ++i)
{
N[i].power_across = curr * curr * N[i].resistance ;
}
}

// This function calculates current
double calcCurrent(double resistance, double voltage)
{
double current;
current = voltage / resistance ;
return current;
}

// This function displays network
void dispNetwork(vector<node>& N)
{
unsigned int i;
for (i = 0; i < N.size() ; ++i)
{
cout << "\n\nParameters: " <<N[i].name<< endl;
cout << "Resistance: " << N[i].resistance << " Ohms" << endl;
cout << "Voltage: " << N[i].voltage_across << " volts" << endl;
cout << "Power: " << N[i].power_across << " Watts" << endl;
}
}

//
int main()
{

double user_volt, total_res , total_curr;
vector<node> N;

// get voltage across the resistor from user
user_volt = getVolt();

// get resistor value from user
total_res = getResistance(N);

// Calculate series current for the one-node network
total_curr = calcCurrent(total_res, user_volt);

// Calculate the Volt across the resistor
calcVolt(N, total_curr);

// Calculate the power across the resistor
calcPower(N, total_curr);

// Display Node information
cout << "\n\nCircuit Parameters:: \n";
cout << "\tTotal Resistance: " << total_res <<" Ohms" << endl;
cout << "\tInput Voltage: " << user_volt <<" Volts" << endl;
cout << "\tSeries Current: " << total_curr << " Amp\n";
cout << "\tTotal Power: "<< total_curr * total_curr * total_res <<" Watts\n" << endl;

dispNetwork(N);

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer

if any query first let me know please,

================================================================

#include <iostream>

#include <string>

#include <vector>

using namespace std;

// You must use this structure

struct node {

string name;

double resistance;

double voltage_across;

double power_across;

};

// An Example of the type of function you may want to create. It take node name

string nodeName() {

string node_name;

cout << "Enter Node Name: ";

cin >> node_name;

return node_name;

}

// This function takes voltage from user

double getVolt()

{

double source_voltage;

cout << "Enter Voltage: ";

cin >> source_voltage;

return source_voltage;

}

void calcVolt(vector<node>& N, double curr)

{

unsigned int i;

for (i = 0; i < N.size() ; ++i)

{

N[i].voltage_across = curr * N[i].resistance ;

}

}

// This function takes resistance from user

double getResistance(vector<node>& N)

{

double resistance , i = 0, sum = 0;

while (1)

{

cout << "Enter Resistance (0 to exit): ";

cin >> resistance;

if (resistance != 0)

{

N.push_back(node());

N[i].name = "Node ";

N[i].resistance = resistance;

sum += resistance;

}

else

return sum;

i++;

}

}

// This function calculates power

void calcPower(vector<node>& N, double curr)

{

unsigned int i;

for (i = 0; i < N.size() ; ++i)

{

N[i].power_across = curr * curr * N[i].resistance ;

}

}

// This function calculates current

double calcCurrent(double resistance, double voltage)

{

double current;

current = voltage / resistance ;

return current;

}

// This function displays network

void dispNetwork(vector<node>& N)

{

unsigned int i;

for (i = 0; i < N.size() ; ++i)

{

cout << "\n\nParameters: " <<N[i].name<< endl;

cout << "Resistance: " << N[i].resistance << " Ohms" << endl;

cout << "Voltage: " << N[i].voltage_across << " volts" << endl;

cout << "Power: " << N[i].power_across << " Watts" << endl;

}

}

//

int main()

{

double user_volt, total_res , total_curr;

vector<node> N;

// get voltage across the resistor from user

user_volt = getVolt();

// get resistor value from user

total_res = getResistance(N);

// Calculate series current for the one-node network

total_curr = calcCurrent(total_res, user_volt);

// Calculate the Volt across the resistor

calcVolt(N, total_curr);

// Calculate the power across the resistor

calcPower(N, total_curr);

// Display Node information

cout << "\n\nCircuit Parameters:: \n";

cout << "\tTotal Resistance: " << total_res <<" Ohms" << endl;

cout << "\tInput Voltage: " << user_volt <<" Volts" << endl;

cout << "\tSeries Current: " << total_curr << " Amp\n";

cout << "\tTotal Power: "<< total_curr * total_curr * total_res <<" Watts\n" << endl;

dispNetwork(N);

// Switch case are use to draw menu

int choice;

bool chooseMenu = true;

while (chooseMenu != false){

cout << "*******************************\n";

cout << " 1 - Change input voltage.\n";

cout << " 2 - Add a single resistor.\n";

cout << " 3 - Edit Resistor.\n";

cout << " 4 - Quit Program.\n";

cout << "*******************************\n";

cout << " Enter your choice : ";

cin >> choice;

switch (choice)

{

// case 1 for change the input voltage

case 1:

cout<<endl;

cout << "You choose change input voltage\n";

// get voltage across the resistor from user

user_volt = getVolt();

// Calculate series current for the one-node network

total_curr = calcCurrent(total_res, user_volt);

// Calculate the Volt across the resistor

calcVolt(N, total_curr);

// Calculate the power across the resistor

calcPower(N, total_curr);

cout << "\n\nCircuit Parameters:: \n";

cout << "\tTotal Resistance: " << total_res <<" Ohms" << endl;

cout << "\tInput Voltage: " << user_volt <<" Volts" << endl;

cout << "\tSeries Current: " << total_curr << " Amp\n";

cout << "\tTotal Power: "<< total_curr * total_curr * total_res <<" Watts\n" << endl;

break;

// case 2 to add single resistor

case 2:

cout<<endl;

cout << "You choose add single register \n";

// get voltage across the resistor from user

cout << "Enter Single Resistor : ";

double single_res;

cin>>single_res;

cout<<endl;

// Calculate series current for the one-node network

total_curr = calcCurrent(single_res, user_volt);

// Calculate the Volt across the resistor

calcVolt(N, total_curr);

// Calculate the power across the resistor

calcPower(N, total_curr);

cout << "\n\nCircuit Parameters:: \n";

cout << "\tTotal Resistance: " << single_res <<" Ohms" << endl;

cout << "\tInput Voltage: " << user_volt <<" Volts" << endl;

cout << "\tSeries Current: " << total_curr << " Amp\n";

cout << "\tTotal Power: "<< total_curr * total_curr * total_res <<" Watts\n" << endl;

break;

// case 3 for edit resistance value

case 3:

cout<<endl;  

cout << "You choose edit resistor \n "<<endl;

// get resistor value from user

total_res = getResistance(N);

// Calculate series current for the one-node network

total_curr = calcCurrent(total_res, user_volt);

// Calculate the Volt across the resistor

calcVolt(N, total_curr);

// Calculate the power across the resistor

calcPower(N, total_curr);

cout << "\n\nCircuit Parameters:: \n";

cout << "\tTotal Resistance: " << total_res <<" Ohms" << endl;

cout << "\tInput Voltage: " << user_volt <<" Volts" << endl;

cout << "\tSeries Current: " << total_curr << " Amp\n";

cout << "\tTotal Power: "<< total_curr * total_curr * total_res <<" Watts\n" << endl;

break;

// case 4 Quit the program

case 4:

cout << "Quit of Program.\n";

chooseMenu = false;

break;

default:

cout << "Not a Valid Choice. \n";

cout << "Choose again.\n";

cin >> choice;

break;

}

}

return 0;

}

==============================================================

Output

E\c++ Examplel testing VoltageTest2.exe nter Uoltage: 230 nter Resistance to exit 1 nter Resistance to exit): 4 nter Resistan

Ectt Example testing voltage lest2.exe 1Change input voltage 2 Hdd a single res istoF 3 Edit Resistor 4 -Quit Progran Enter y

===================================================================================

Please let me know if you have any query

Add a comment
Know the answer?
Add Answer to:
Add a menu to the existing code already written to contain the following options Change Input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly....

    Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly. Hello there. I am trying to implement the djikstras shortest path algorithm into my code in C++ with a vector of vectors. However, when I test I get an error "Exception thrown at 0x75FFC762 in graphMain.exe: Microsoft C++ exception: std::out_of_range at memory location 0x009CF26C" for line 115 in graph.cpp. Could you help me debug? I am so close! ----------------------------FILE NAME: pch.h--------------------------------------- // pch.cpp: source...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • The second picture that I attached is the input and output. I already did the code...

    The second picture that I attached is the input and output. I already did the code but I also have to add the partition number assigned to the job (if the job was allocated). Can you please do that for me? I copied and pasted my code below. #include <iostream> #include <string.h> #include <stdio.h> using std::cout; using std::cin; using std::endl; unsigned int memorySize; // Function prototypes unsigned int FirstFit(struct Partition *, int, struct Job *, int); unsigned int BestFit(struct Partition...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • 1. in this programe i want add some products to be shown after choosing choise (...

    1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...

  • class AVLTree The following functions are the minimum requirements for the AVL class. You can add...

    class AVLTree The following functions are the minimum requirements for the AVL class. You can add any function from Assignment 2 to this class. You should modify the BSTree insert function so that the tree remains balanced after each insertion. Required Public Member Functions void insert(const string &): Insert an item to the binary search tree and perform rotation if necessary. int balanceFactor(Node*): Return the balance factor of a given node. void printBalanceFactors(): Traverse and print the tree in inorder...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT