Hey so im making a program to figure out a energy cost my program so far is, but I want the watt-hours to be for all three appliances not just one how do i do that?:
#include <iostream>
using namespace std;
int main ()
{
double watts, hours_per_day, watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day;
cout << "How many Watts for the Television? " ;
cin >> watts;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day;
cout << "How many Watts for the Washer? " ;
cin >> watts;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day;
watt_hours = watts * hours_per_day * 30;
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
You will have to use a stand-alone watts variable and hours_per_day for each appliance.
Also consider using loops for doing such, it makes code more clearer and less sized;
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
static const double dollars_per_wh = 0.00008;
int main(int argc, char ** argv)
{
int num_apps = 3;
std::vector<std::string> v_apps;
v_apps.push_back("Air conditioner");
v_apps.push_back("Television");
v_apps.push_back("Washer");
double watt, hours, watt_hours = 0;
for(register int i = 0; i < num_apps; ++i)
{
cout << "How many watts for the " << v_apps[i].c_str() << "? ";
cin >> watt;
cout << "How many hours/day do you run the " << v_apps[i].c_str() << "? ";
cin >> hours;
watt_hours = watt_hours + ((watt * hours) * 30);
}
cout << "You have used " << watt_hours << " Watt-Hours of electricity." << endl;
return 0;
}
I guess you could do the following with triple variables:
#include <iostream>
using namespace std;
int main ()
{
double watts_ac, watts_tv, watts_wa;
double hours_per_day_ac, hours_per_day_tv, hours_per_day_wa;
double watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts_ac;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day_ac;
cout << "How many Watts for the Television? " ;
cin >> watts_tv;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day_tv;
cout << "How many Watts for the Washer? " ;
cin >> watts_wa;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day_wa;
watt_hours = (watts_ac * hours_per_day_ac + watts_tv * hours_per_day_tv + watts_wa * hours_per_day_wa) * 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}
or just aggregate data:
#include <iostream>
using namespace std;
int main ()
{
double watts, hours_per_day_ac, watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day;
watts_hours = watts*hours_per_day;
cout << "How many Watts for the Television? " ;
cin >> watts;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day;
watts_hours += watts*hours_per_day;
cout << "How many Watts for the Washer? " ;
cin >> watts;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day;
watts_hours += watts*hours_per_day;
watt_hours *= 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}
Related
I have to write a program in C++ that asks user to enter lines and store them in a text file.
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
}
The problem is that the text is not being stored in different lines.
I have to use Turbo C++ for this program.
Minimal, Reproducible Example:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
void show(char name[80])
{
char line[800];
cout << "Contents of the file:" << endl << endl;
ifstream fin (name);
while(!fin.eof())
{
fin.getline(line,80);
if(fin.eof())
break;
cout << line;
}
}
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
fflush(stdin);
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
fflush(stdin);
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
show(name);
}
int main()
{
char name1[80];
cout <<"Enter the name of the text file:" << endl;
gets(name1);
create_file(name1);
return 0;
}
O.F community! I'm very new to c++ and I decided to create my own game.
My issue is that at one point in the game the player is asked to answer 3 things:
-Gender
-Age
-Setting
void assessment()
{
cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
cout << "Press any key to continue\n\n";
cin >> anykey;
cout << "You are a " << ageNumber << " year old " << gender; //incomplete - needs //<< setting;
}
The issue is with gender not being displayed. It can cout << ageNumber fine. But when the game is played and this part is reached this is what it says:
Now that the characteristics have been selected. Let's review what they are:
Press any key to continue
1
You are a 16(that is what I put for ageNumber earlier on) year old . <- Notice how "old" and "." have a space? As if the string gender is read as a blank.
EDIT: It is supposed to say:
You are a 16 year old male/female/it.
This is the part that determines the gender.
void fate()
{
cout << "Before we begin, a few characteristics must be selected\n\n";
cout << "Gender?\n\n";
cout << "1. Male\n";
cout << "2. Female\n";
cout << "3. Other\n\n";
cin >> genderNumber;
cout << "Now your next characteristic...Press any key to continue\n\n";
cin >> anykey;
cout << "What is your age? (Must be younger than 50 to play)\n\n";
cin >> ageNumber;
setting2();
if (genderNumber == 1)
{
string gender = "male";
}
else if (genderNumber == 2)
{
string gender = "female";
}
else if (genderNumber == 3)
{
string gender = "'it'";
}
else
{
"invalid input\n\n\n\n\n\n";
fate();
}
===========================================================================================
This is the full code:
#include <iostream>
#include <string>
using namespace std;
void rules();
void controls();
void start();
void fate();
void assessment();
void setting2();
int fateNumber;
int anykey;
int ageNumber;
int settingNumber;
int genderNumber;
string gender;
string setting;
int main(int argc, char** argv)
{
cout << "===W E L C O M E T O C H O O S E Y O U R F A T E=== \n\n";
cout << "Press any key to continue\n";
cin >> anykey;
start();
}
void controls()
{
cout << "Throughout this game will you be making choices, each choice will have a corresponding number next to it. Press the letter to the corresponding choice you want to make. Press any key to continue\n";
cin >> anykey;
cout << "Here is an example...you are a cat, you can either\n\n";
cout << "1. Meow\n\n";
cout << "2. Hiss\n";
cout << "Press any key to continue\n";
cin >> anykey;
cout << "If I wanted to Hiss I would press the 2 key\n\n";
cout << "Press any key to continue on to the rules\n\n";
cin >> anykey;
rules();
}
void rules()
{
cout << "#1 You will be given a set number of choices. Press any key to continue\n\n";
cin >> anykey;
cout << "#2: There are 2 factors to this game. Press any key to continue\n\n";
cin >> anykey;
cout << "#3: The first factor is that either you live or you die. Press any key to continue\n\n";
cin >> anykey;
cout << "#4: The second factor is that if you live, your character will be judged based on events you've made. You can still live, but you might have a good or bad effect on the people around you. Press any key to continue\n\n";
cin >> anykey;
cout << "Let's play shall we?";
}
void start()
{
cout << "You shall begin a journey that'll decide either you make it or you don't. Press any key to continue\n\n";
cin >> fateNumber;
cout << "But before we start...The game needs to be explained.\n\n\n\n";
cout << "Controls(1 key)---------------------------Rules(2 key)---------------Skip(3 key) \n\n";
cin >> fateNumber;
if (fateNumber == 1)
{
controls();
}
else if (fateNumber == 2)
{
rules();
}
else if (fateNumber == 3)
{
fate();
}
else
{
cout << "invalid key\n\n\n\n\n\n\n";
start();
}
}
void fate()
{
cout << "Before we begin, a few characteristics must be selected\n\n";
cout << "Gender?\n\n";
cout << "1. Male\n";
cout << "2. Female\n";
cout << "3. Other\n\n";
cin >> genderNumber;
cout << "Now your next characteristic...Press any key to continue\n\n";
cin >> anykey;
cout << "What is your age? (Must be younger than 50 to play)\n\n";
cin >> ageNumber;
setting2();
if (genderNumber == 1)
{
string gender = "male";
}
else if (genderNumber == 2)
{
string gender = "female";
}
else if (genderNumber == 3)
{
string gender = "'it'";
}
else
{
"invalid input\n\n\n\n\n\n";
fate();
}
if (ageNumber >= 50 && ageNumber < 100)
{
cout << "Didn't you read the age rating dumbass? Younger than 50...\n\n";
}
else if (ageNumber < 50 && ageNumber > 22)
{
setting2();
}
else if (ageNumber <= 22 && ageNumber >= 16)
{
setting2();
}
else if (ageNumber < 16 && ageNumber >= 0)
{
setting2();
}
else if (ageNumber >= 100)
{
cout << "More than a century old and you expect me to believe you got this far? Bullshit.";
}
else
{
cout << "Error - Invalid number\n\n\n\n\n\n";
cout << "what is your age again?\n\n";
cin >> ageNumber;
}
}
void assessment()
{
cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
cout << "Press any key to continue\n\n";
cin >> anykey;
cout << "You are a " << ageNumber << " year old " << gender << ". You are " << setting << endl << endl;
if (settingNumber == 1)
{
string setting = "home";
}
else if (settingNumber == 2)
{
string setting = "jail";
}
}
void setting2()
{
cout << "Alright and lastly pick your setting\n\n";
cout << "1. Home sweet home\n";
cout << "2. Vacation \n";
cout << "3. Jail\n\n";
cin >> settingNumber;
assessment();
}
=======================================================================================
Any help is appreciated ^^ Code on!
Output (thru ofstream-s like cout) is buffered in C++. You want
cout << flush;
or
cout << endl;
to ask the buffer to be flushed (endl manipulator also emits a new line but flush does not).
So code:
cout << "You are a " << ageNumber << " year old " << gender << flush;
or
cout << "You are a " << ageNumber << " year old " << gender << endl;
Also, your gender should be declared at a wider scope:
if (genderNumber == 1) {
string gender = "male";
}
should become (to refer to the global gender)
if (genderNumber == 1) {
gender = "male";
}
BTW, if you enabled all warnings with your compiler (e.g. compile with g++ -g -Wall -Wextra, if using GCC, e.g. on Linux) you should have been warned. And of course, you should use the debugger (e.g. gdb, at least on Linux) and step into your code with it.
#include <iostream>
#include <iomanip>
using namespace std;
int main () // print to console: 3.0*5.0=15.00
{
double a;
double b;
a =(3.0);
b =(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "------" << endl;
cout << fixed << setprecision (2) << a*b << "\n" << endl;
return 0;
}
int calculate () // print to console: (7.1*8.3)-2.2=56.73
{
double a;
double b;
double c;
a = (7.1);
b = (8.3);
c = (2.2);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "- " << c << "\n" << endl;
cout << "------" << endl;
cout << setprecision(2) << (a*b)-c << "\n" << endl;
}
int calculation () // print to console: 3.2/(6.1*5.0)=0.10
{
double a;
double b;
double c;
a=(3.2);
b=(6.1);
c=(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << b << "*" << c << endl; //how can I use variables instead of using quotes?
cout << "------" << endl;
cout << setprecision(2) << a/(b*c) << "\n" << endl;
system("PAUSE");
return 0;
}
What does this output error mean? How do I fix it? someone please explain this to me. Am I suppose to add: int calculate(int a, int b, int c)?
Output:
(32): error C4716: 'calculate' : must return a value
You've declared your function as one that returns an int value but there's no return statement. Try changing the declaration to void calculate() if you don't need to return a value from it.
The calculate function needs to return a value.
You need to add something like this at the end of calculate:
return (a*b)-c;
It's only because your calculate function is supposed to return an int
and there's no return in your function.
if you don't want to return anything, you can put void calculate() instead
I am new to C++ and this is like my first program I made and I used Visual C++ 2010 Express.
It is a weight conversion thing. There is an if loop, an else if loop and an else.
Here is the code:
#include <iostream>
using namespace std;
int main() {
float ay,bee;
char char1;
cout << "Welcome to the Ounce To Gram Converter" << endl << "Would you like to convert [O]unces To Grams or [G]rams To Ounces?" << endl;
start:
cin >> char1;
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
else if (char1 = "o"||"O"){
cout << "How many ounces would you like to convert" << endl;
cin >> ay;
cout << ay << " ounces is equal to: " << ay/0.035274 << " grams." << endl; goto start;
}
else{
cout << "Error 365457 The character you entered is to retarded to comprehend" << endl;
goto start;
}
cin.ignore();
cin.get();
return 0;
}
If I enter a "g", it executes this:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
like it should
However, if I enter an "o", it executes this:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
Instead of this:
else if (char1 = "o"||"O"){
cout << "How many ounces would you like to convert" << endl;
cin >> ay;
cout << ay << " ounces is equal to: " << ay/0.035274 << " grams." << endl; goto start;
}
Even if I put something random, like "h"
This Happens:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
Instead of this:
else{
cout << "Error 365457 The character you entered is to retarded to comprehend" << endl;
goto start;
}
Please tell me what I did wrong.
char1 = "o"||"O" will always evaluate to true, because "O" is not null.
You want to use char1 == 'o' || char == 'O' and similar all over your if statements.
Note that = is assignment and == is an equality check. Use == when testing for equality and = when assigning. C and C++ allows you to use = in a check which returns the value of the assignment. This value is not 0, which evaluates to true and thus your if statement executes.
I know this is very vague but im new to C++. Im making a calculator as a first time project, and what im wanting to do is, if replied to with 'y', make the script rerun from the beginning...basically.
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
cout << "Hello and Welcome to the Test Calculator!\n";
signed char choice;
char resp;
cout << "Choose your problem:\n a)Addition\n b)Subtraction\n c)Multiplication\n d)Division\n e)Square Root\n f)Hypotenuse\n";
scanf ("%c", &choice);
switch (choice)
{
case 'a':
{
int a;
int b;
cout << "Addition\n";
cout << "Please enter a number:\n";
cin >> a;
cout << "Please enter your second number:\n";
cin >> b;
cin.ignore();
int result = a + b;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result;
cin.get();
break;
}
case 'b':
{
int c;
int d;
cout << "Subtraction\n";
cout << "Please enter a number:\n";
cin >> c;
cout << "Please enter your second number:\n";
cin >> d;
cin.ignore();
int result2 = c - d;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result2;
cin.get();
break;
}
case 'c':
{
int e;
int f;
cout << "Multiplication\n";
cout << "Please enter a number:\n";
cin >> e;
cout << "Please enter your second number:\n";
cin >> f;
cin.ignore();
int result3 = e * f;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result3;
cin.get();
break;
}
case 'd':
{
int g;
int h;
cout << "Division\n";
cout << "Please enter a number:\n";
cin >> g;
cout << "Please enter your second number:\n";
cin >> h;
cin.ignore();
int result4 = g / h;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result4;
cin.get();
break;
}
case 'e':
{
int x;
#define square ((x)*(x))
cout << "Square Root\n";
cout << "Please enter a number:\n";
cin >> x;
cin.ignore();
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<square;
cin.get();
break;
}
case 'f':
{
int i;
int j;
cout << "Hypotenuse\n";
cout << "Enter your smaller side:\n";
cin >> i;
cout << "Please enter the longer side:\n";
cin >> j;
cin.get();
int hypotenuse = ((i*i)+(j*j));
cout << "Calculating...\n";
cout << "The hypotenuse is the square root of:\n"<<" "<<hypotenuse;
cin.ignore();
cout << "Would you like to do another problem?\n y)Yes\n n)No\n";
cin >> resp; //this is where im trying to test this at
}
default:
{
cout << " \n";
cout << "Error: Undefined response\n";
cout << "Contact the source programmer for details\n";
}
}
}
If you put the switch into a while loop that checks to see if the char == 'n' it will continue until it finds it.
while(choice != 'n')
{
switch (choice)
{
case 'a':
{
int a;
int b;
cout << "Addition\n";
cout << "Please enter a number:\n";
cin >> a;
cout << "Please enter your second number:\n";
cin >> b;
cin.ignore();
int result = a + b;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result;
cin.get();
break;
}
case 'b':
{
int c;
int d;
cout << "Subtraction\n";
cout << "Please enter a number:\n";
cin >> c;
cout << "Please enter your second number:\n";
cin >> d;
cin.ignore();
int result2 = c - d;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result2;
cin.get();
break;
}
case 'c':
{
int e;
int f;
cout << "Multiplication\n";
cout << "Please enter a number:\n";
cin >> e;
cout << "Please enter your second number:\n";
cin >> f;
cin.ignore();
int result3 = e * f;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result3;
cin.get();
break;
}
case 'd':
{
int g;
int h;
cout << "Division\n";
cout << "Please enter a number:\n";
cin >> g;
cout << "Please enter your second number:\n";
cin >> h;
cin.ignore();
int result4 = g / h;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result4;
cin.get();
break;
}
case 'e':
{
int x;
#define square ((x)*(x))
cout << "Square Root\n";
cout << "Please enter a number:\n";
cin >> x;
cin.ignore();
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<square;
cin.get();
break;
}
case 'f':
{
int i;
int j;
cout << "Hypotenuse\n";
cout << "Enter your smaller side:\n";
cin >> i;
cout << "Please enter the longer side:\n";
cin >> j;
cin.get();
int hypotenuse = ((i*i)+(j*j));
cout << "Calculating...\n";
cout << "The hypotenuse is the square root of:\n"<<" "<<hypotenuse;
cin.ignore();
cout << "Would you like to do another problem?\n y)Yes\n n)No\n";
cin >> choice; //this is where im trying to test this at
}
}
}
You can add another option to continue calculator. something like this:
while(doContinue == true){
switch{.....}
}
that is, enclose the switching part of code in a while loop. Set doContinue as true at first then change it at the end according to the user input.