system("cls") doesnt work, it finishes the program without printing anything.
the program works without it but doesnt look nice in the console without all the previous prints cleared. I've tried putting the command in different places but it doesnt matter where i put it the program finishes instantly.
#include <iostream>
#include <math.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;
float c, temp, eq;
char odp = 't';
int main()
{
do
{
cout << "Choose from what value you'd like to convert." << endl;
cout << "Celsius = 1 ; Fahrenheit = 0" << endl;
cin >> c;
if (c==1)
{
cout << "Insert Celsius value: ";
cin >> temp;
eq = temp * 1.8 + 32;
cout << "It is " << eq << " degrees Fahrenheit." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else if (c==0)
{
cout << "Insert Fahrenheit value: ";
cin >> temp;
eq = (temp * 1.8) - 32;
cout << "It is " << eq << " degrees Celsius." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else
{
cout << "Please choose correctly" << endl;
}
}
while(odp = 't');
return 0;
}
how i put the command and it finishes the whole program instantly
#include <iostream>
#include <math.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;
float c, temp, eq;
char odp = 't';
int main()
{
do
{
cout << "Choose from what value you'd like to convert." << endl;
cout << "Celsius = 1 ; Fahrenheit = 0" << endl;
cin >> c;
if (c==1)
{ system("cls") //here
cout << "Insert Celsius value: ";
cin >> temp;
eq = temp * 1.8 + 32;
cout << "It is " << eq << " degrees Fahrenheit." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else if (c==0)
{
cout << "Insert Fahrenheit value: ";
cin >> temp;
eq = (temp * 1.8) - 32;
cout << "It is " << eq << " degrees Celsius." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else
{
cout << "Please choose correctly" << endl;
}
}
while(odp = 't');
return 0;
}
i really dont know, thanks for the replies
Related
How do you provide a condition in the if statement, if the variable is an integer data type then it will be displayed, and if the variable is any other data type then something else will be displayed?
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int main() {
char pilih,pilih2;
int p, l, a, t, r,ulang = 4;
float luas1, luas2, luas3, keliling1, keliling2, keliling3, phi = 3.14;
while (ulang > 0) {
pilihUlang:
cout << "Pilih jenis bangun datar berikut :\n 1. Persegi panjang\n 2. Segitiga sama sisi\n 3. Lingkaran" << endl;
cout << "Masukan pilihan anda [1/2/3] : ";
cin >> pilih;
system("cls");
switch (pilih) {
case '1':
system("color 07");
cout << "Luas Dan Keliling Persegi Panjang" << endl;
cout << "Masukan Panjang = ";
cin >> p;
if (?) { // <-- here
cout << "Masukan Lebar = ";
cin >> l;
system("cls");
cout << "Luas dan keliling Persegi Panjang dengan panjang " << p << " dan lebar " << l << ", yaitu :" << endl;
luas1 = p * l;
keliling1 = 2 * (p + l);
cout << "Luas = " << luas1 << endl;
cout << "Keliling = " << keliling1 << endl;
cout << "Sisa bisa memilih ulang " << ulang - 1 << " kali." << endl;
break;
}
else {
//...
}
From the istream::operator>> man page:
If extraction fails (e.g. if a letter was entered where a digit is
expected), zero is written to value and failbit is set.
So, your function could test the cin.good() method to see if the >> operation was successful, like this:
cin >> p;
if (cin.good()) {
cout << "The integer you typed was " << p << endl;
} else {
cout << "Hey, that wasn't an integer!" << endl;
}
I have a Problem. I'm Trying to make a regular Calculator and Shape's Area and Perimeter finder.It's a combination. I didn't start on my Shape's Area and Perimeter Finder. This is My main.cpp.
#include <iostream>
#include <string>
#include "AAPO.h" // Its a Header File.
using namespace std;
void Calculators_Operation();
int main()
{
string opera;
cout << "Do you want Arithmetic Calculator or Area and Perimeter Calculator"
<< endl;
cin >> opera;
if (opera == "Arithmetic Calculator" or "arithmetic calculator" or "AC")
{
Calculators_Operation();
}
return 0;
}
This is my Operation Chooser.
#include <iostream>
#include <string>
#include "Arithmetic Chooser.h"
using namespace std;
void Calculators_Addition();
void Calculators_Subtraction();
void Calculators_Multiplication();
void Calculators_Division();
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" or "addition" or "+");
{
Calculators_Addition();
};
if (answera == "Subtraction" or "subtraction" or "-");
{
Calculators_Subtraction();
};
if (answera == "Multiplication" or "multiplication" or "*" or "x" or "X")
{
Calculators_Multiplication();
};
if (answera == "Division" or "division" or "/")
{
Calculators_Division();
};
return;
}
This is my AAPO.h.
#ifndef AAPO_H_INCLUDED
#define AAPO_H_INCLUDED
void Calculators_Operation();
#endif // AAPO_H_INCLUDED
My Addition.
#include <iostream>
#include <string>
using namespace std;
void calculators_Addition_2();
void calculators_Addition_3();
void calculators_Addition_4();
void calculators_Addition_5();
void Calculators_Addition()
{
//ADDITION COMPLETE
string numberadd;
cout << "How much numbers do you want?" << endl;
cin >> numberadd;
if (numberadd == "2")
{
calculators_Addition_2();
return;
};
if (numberadd == "3")
{
calculators_Addition_3();
return;
};
if (numberadd == "4")
{
calculators_Addition_4();
return;
};
if (numberadd == "5")
{
calculators_Addition_5();
return;
}
}
void calculators_Addition_2()
{
int add11;
int add12;
int sum;
cout << "Enter the first number" << endl;
cin >> add11;
cout << "Enter the second number" << endl;
cin >> add12;
sum = add11 + add12;
cout << "The sum of the numbers are " << sum << endl;
return;
}
void calculators_Addition_3()
{
int add13;
int add23;
int add33;
int sum2;
cout << "Enter the First Number" << endl;
cin >> add13;
cout << "Enter the Second Number" << endl;
cin >> add23;
cout << "Enter the Third Number" << endl;
cin >> add33;
sum2 = add13 + add23 + add33;
cout << "The Sum of the Numbers are " << sum2 << endl;
return;
}
void calculators_Addition_4()
{
int add14;
int add24;
int add34;
int add44;
int sum3;
cout << "Enter the First Number" << endl;
cin >> add14;
cout << "Enter the Second Number" << endl;
cin >> add24;
cout << "Enter the Third Number" << endl;
cin >> add34;
cout << "Enter the Fourth Number" << endl;
cin >> add44;
sum3 = add14 + add24 + add34 + add44;
cout << "The Sum of the Numbers are " << sum3 << endl;
return;
}
void calculators_Addition_5()
{
int a15;
int a25;
int a35;
int a45;
int a55;
int sum4;
cout << "Enter the First Number" << endl;
cin >> a15;
cout << "Enter the Second Number" << endl;
cin >> a25;
cout << "Enter the Third Number" << endl;
cin >> a35;
cout << "Enter the Fourth Number" << endl;
cin >> a45;
cout << "Enter the Fifth Number" << endl;
cin >> a55;
sum4 = a15 + a25 + a35 + a45 + a55;
cout << "The Sum of the Numbers are " << sum4 << endl;
return;
}
My Subtraction.
#include <iostream>
using namespace std;
void Calculators_Subtraction()
{
int subractify;
int subracta;
int differencea;
cout << "Type in the First Number!" << endl;
cin >> subractify;
cout << "Type in the Second Number!" << endl;
cin >> subracta;
differencea = subractify - subracta;
cout << "The Difference is " << differencea << endl;
return;
}
My Multiplication.
#include <iostream>
#include <string>
using namespace std;
void Calculators_Multiplication_2();
void Calculators_Multiplication_3();
void Calculators_Multiplication_4();
void Calculators_Multiplication_5();
void Calculators_Multiplication()
{
string multicipia;
cout << "How much numbers do you want?" << endl;
cin >> multicipia;
if (multicipia == "2" or "Two" or "two")
{
Calculators_Multiplication_2();
};
if (multicipia == "3" or "Three" or "three")
{
Calculators_Multiplication_3();
};
if (multicipia == "4" or "Four" or "four")
{
Calculators_Multiplication_4();
};
if (multicipia == "5" or "Five" or "five")
{
Calculators_Multiplication_5();
};
return;
}
void Calculators_Multiplication_2()
{
int multi2a;
int multi2b;
int product2;
cout << "Type in the First Number." << endl;
cin >> multi2a;
cout << "Type in the Second Number." << endl;
cin >> multi2b;
product2 = multi2a * multi2b;
cout << "The Product is " << product2 << "." << endl;
return;
}
void Calculators_Multiplication_3()
{
int multi3a;
int multi3b;
int multi3c;
int product3;
cout << "Enter the First Number!" << endl;
cin >> multi3a;
cout << "Enter the Second Number!" << endl;
cin >> multi3b;
cout << "Enter the Third Number!" << endl;
cin >> multi3c;
product3 = multi3a * multi3b * multi3c;
cout << "The Product is" << product3 << "." << endl;
return;
}
void Calculators_Multiplication_4()
{
int multi4a;
int multi4b;
int multi4c;
int multi4d;
int product4;
cout << "Enter the First Number!" << endl;
cin >> multi4a;
cout << "Enter the Second Number!" << endl;
cin >> multi4b;
cout << "Enter the Third Number!" << endl;
cin >> multi4c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi4b;
product4 = multi4a * multi4b * multi4c * multi4d;
cout << "The Product of the Numbers are " << product4 << "!" << endl;
return;
}
void Calculators_Multiplication_5()
{
int multi5a;
int multi5b;
int multi5c;
int multi5d;
int multi5e;
int product5;
cout << "Enter the First Number!" << endl;
cin >> multi5a;
cout << "Enter the Second Number!" << endl;
cin >> multi5b;
cout << "Enter the Third Number!" << endl;
cin >> multi5c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi5d;
cout << "Enter the Fifth Number!" << endl;
cin >> multi5e;
product5 = multi5a * multi5b * multi5c * multi5d * multi5e;
cout << "The Product of the Numbers are" << product5 << "!" << endl;
return;
}
My Division.
#include <iostream>
using namespace std;
void Calculators_Division()
{
float divisia;
float divisiab;
float quotient;
cout << "Enter the Divisor" << endl;
cin >> divisia;
cout << "Enter the Dividend" << endl;
cin >> divisiab;
quotient = divisia / divisiab;
cout << "The Quotient of the Numbers are " << quotient << endl;
return;
}
The Problem right now is that when addition finishes , subtraction starts. After Subtraction, Multiplication. After Multiplication, Division. After That the program ends. I'm sorry. Its just that I'm new to programming (Like one month).
There are many things to improve (some of them already mentioned in comments), but exact answer to your question is:
you have semicolon after your if-statement lines in Calculators_Operation() function body, which makes if-statements useless and your "operation" functions are being called every time (what you already noticed).
you use "or" operator in incorrect way, if you use (answera == "addition" or "+"), you basically say: ((answera == "addition) or "+") and that evaluates to true (see here)
EDIT:
Your function Calculators_Operation may be changed to:
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" || answera == "addition" || answera == "+")
{
Calculators_Addition();
}
else if (answera == "Subtraction" || answera == "subtraction" || answera == "-")
{
Calculators_Subtraction();
}
else if (answera == "Multiplication" || answera == "multiplication" || answera == "*" || answera == "x" || answera == "X")
{
Calculators_Multiplication();
}
else if (answera == "Division" || answera == "division" || answera == "/")
{
Calculators_Division();
} else {
cout << "Unknown operation entered!" << endl;
}
}
NOTE: all your if statements need to be changed according to this function.
If anything does not work, please either put in comments what exactly does not work or edit your question with new code posting and question.
After compiling a console application and entering wrong data it gives strange output value, like 2.0434e-006, while it was requesting numerals. Here is the code:
#include <iostream>
#include <conio.h>
int main() {
using namespace std;
float l,w,h;
float s;
cout << "\nCalculating surface area of the parallelepiped\n";
cout << "Enter the raw data:\n";
cout << "Length (cm) -> ";
cin >> l;
cout << "Width (cm) -> ";
cin >> w;
cout << "Height (cm) -> ";
cin >> h;
s = (l*w + l*h + w*h)*2;
cout << "Surface area: " << s << " sq. cm\n";
cout << "\n\nPress any key...";
getch();
}
I heared something about IEEE 754 floating-point faults, but even this information doesn't make me sure in my knowledge.
Values of uninitialized non-static local variables are indeterminate.
Check if the input succeeded and handle errors.
#include <iostream>
int main() {
using namespace std;
float l,w,h;
float s;
cout << "\nCalculating surface area of the parallelepiped\n";
cout << "Enter the raw data:\n";
cout << "Length (cm) -> ";
if (!(cin >> l)) {
cout << "input error\n";
return 1;
}
cout << "Width (cm) -> ";
if (!(cin >> w)) {
cout << "input error\n";
return 1;
}
cout << "Height (cm) -> ";
if (!(cin >> h)) {
cout << "input error\n";
return 1;
}
s = (l*w + l*h + w*h)*2;
cout << "Surface area: " << s << " sq. cm\n";
cout << "\n\nPress any key...";
}
So I'm trying to create a feature that will let me quit my game once you've completed it, or you have the option to play again. The replay option works, but whenever I try the quit part, it just goes back to the start of the game.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
int main() {
SetConsoleTitle("Guess the number! | v1.0");
int replay;
replay = 1;
int g, n;
play:
g = NULL;
srand(time(NULL));
n = rand() % 20 + 1;
system("cls");
cout << "Number guessing game" << endl;
cout << "********************" << endl;
cout << endl;
cout << "The random number has been generated. It is between 1 and 20" << endl;
system("pause");
while (g != n) {
system("cls");
cout << "Number Guessing Game" << endl;
cout << "********************" << endl;
cout << endl;
cout << "Type a guess between 1 and 20 then press ENTER" << endl;
cout << "Your guess: ";
cin >> g;
}
if (g = 1113) {
goto debugskip;
}
debugskip:
system("cls");
cout << "You have found the number!" << endl;
cout << "The number was " << n << endl;
cout << "Would you like to play again? 1 for yes, 2 for no.";
cin >> replay;
if (replay = 1) {
goto play;
}
else if (replay = 2) {
exit(1);
}
return 0;
}
You are using assignment operator instead of equals operator in your ifs.
Change
if (replay = 1) {
for
if (replay == 1) {
And do the same in the other places with the same problem.
I have an Addressbook C++ program that compiles and everything, but I cannot figure out how to write it to a file that saves the data each time it exits. Here is my code:
//AddressBook Program
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <stdio.h>
using namespace std;
class AddressBook{
public :
AddressBook()
{
count = 0;
}
void AddEntry();
void DisplayAll();
void DisplayEntry(int i); // Displays one entry
void SearchEntry();
int MainMenu();
struct Entry_Struct
{
char firstName[ 15 ] ;
char lastName[ 15 ] ;
char birthday[ 15 ] ;
char phone[ 15 ] ;
char email[ 15 ] ;
};
Entry_Struct entries[100];
unsigned int count;
};
void AddressBook::AddEntry()
{
cout << "Entry number " << (count + 1) << " : " << endl;
cout << "Enter First Name: ";
cin >> entries[count].firstName;
cout << "Enter Last Name: ";
cin >> entries[count].lastName;
cout << "Enter Date of Birth: ";
cin >> entries[count].birthday;
cout << "Enter Phone Number: ";
cin >> entries[count].phone;
cout << "Enter Email: ";
cin >> entries[count].email;
++count; // tally total entry count
}
void AddressBook::DisplayEntry(int i)
{
cout << "Entry[" << i + 1 << "] : " << endl; // states # of entry
cout << "First name : " << entries[i].firstName << endl;
cout << "Last name : " << entries[i].lastName << endl;
cout << "Date of birth : " << entries[i].birthday << endl;
cout << "Phone number : " << entries[i].phone << endl;
cout << "Email: " << entries[i].email << endl;
}
void AddressBook::DisplayAll()
{
cout << "Number of entries : " << count << endl;
for(int i = 0;i < count;++i)
DisplayEntry(i);
}
void AddressBook::SearchEntry()
{
char lastname[32];
cout << "Enter last name : ";
cin >> lastname;
for(int i = 0;i < count;++i)
{
if(strcmp(lastname, entries[i].lastName) == 0)
{
cout << "Found ";
DisplayEntry(i);
cout << endl;
}
}
}
// Your class
AddressBook my_book;
int MainMenu()
{
int num;
bool bQuit = false;
// Put all your code into a while loop.
while(bQuit == false)
{
cout << "+-------------------------------------+" << endl;
cout << "| Address Book Menu |" << endl;
cout << "| |" << endl;
cout << "| 1- Add an entry |" << endl;
cout << "| 2- Search for an entry by last name |" << endl;
cout << "| 3- Display all entries |" << endl;
cout << "| 4- Exit |" << endl;
cout << "| |" << endl;
cout << "+-------------------------------------+" << endl;
cout << endl;
cout << "Please enter a number for one of the above options: ";
cin >> num;
cout << endl;
if (num == 1)
my_book.AddEntry();
else if (num == 2)
my_book.SearchEntry();
else if (num == 3)
my_book.DisplayAll();
else if (num == 4)
bQuit = true;
else
cout << "Invalid choice. Please try again" << endl;
cout << endl;
}
return 0;
}
int main (){
MainMenu();
return 0;
}
I've gone over my textbook all day and nothing I'm doing is working.
Here is a basic example of opening, and writing an output file,
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream outfile;
outfile.open ("addressbook.txt");
outfile << "Daffy,Duck,123 Main Street,Anytown,OH,USA,123-456-7890\n";
myfile.close();
return 0;
}
You need to have an inserter for your class. It's done using operator <<:
// Inside your class:
friend std::istream& operator<<(std::ostream& os, const AddressBook ab)
{
return os << /* ... */
}
As you can see, a user-defined operator << can be implemented in terms of the pre-defined standard inserter. You can use it in the following way:
std::ifstream in("yourtxtfile.txt");
in << my_book;