#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
Related
So, I had my dates stored as a string, and loop through it but it cannot find the matching date from the vector. To make sure it loops through the vector, I put else at the bottom there and for every time it loops through the vector and is not found the date will display it.
[1] https://i.stack.imgur.com/KA17B.png
cout << "Please enter a date: ";
cin >> searchByDate;
for (columnCount = 0; columnCount < currentRow; columnCount++)
{
if (searchByDate == DATE[columnCount])
{
cout << DATE[columnCount] << "\t" << CUSTNAME[columnCount] << "\t" << GENDER[columnCount] << "\t" << IDENTITY[columnCount] << "\t";
cout << TELEPHONE[columnCount] << "\t" << TYPE[columnCount] << "\t" << THERAPIST[columnCount] << "\t" << CHARGES[columnCount] << endl;
totalRevenue += CHARGES[columnCount];
}
else
cout << "date 404";
Help! Emergency!
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;
}
I am trying to get the program to read 2 string then convert them to ASCII value and compare the 2 value.
I am not sure how to change the string to ASCII value for the comparing function to work.
#include <iostream>
int main()
{
char ch[50];
std::cout << "Enter a character: ";
std::cin.getline(ch, sizeof(ch));
std::cout << "ASCII Value of " << ch << " is " << int(ch) << std::endl;
char cha[50];
std::cout << "Enter a character: ";
std::cin >> cha;
std::cout << "ASCII Value of " << cha << " is " << int(cha) << std::endl;
if (ch[50] == cha[50]) {
std::cout << "ASCII Value of " << cha << " is " << "equal to " << ch;
return 0;
}
else if (ch > cha) {
std::cout << ch << " is greater than " << cha << std::endl;
std::cout << int(ch) - int(cha) << "is the differents between the two " ;
return 1;
}
else
{
std::cout << ch << " is less than " << cha;
}
system("PAUSE");
}
char ch[50];
std::cout << "Enter a character: ";
std::cin.getline(ch, sizeof(ch));
char ch[50] is "array of character" otherwise known as "string"
if (ch[50] == cha[50]) { ... }
Both ch and ch50 have 50 elements. The index starts at zero, that means the last valid element is ch[49]. ch[50] is buffer overflow and causes undefined behavior.
You want instead to declare a single character, char ch; and read that character. Or declare ch[50] and read the string, and do a comparison for ch[0] (that's the first element in the index)
int main()
{
char ch;
char cha;
std::cout << "Enter one character: ";
std::cin >> ch;
std::cout << "ASCII Value of " << ch << " is " << int(ch) << std::endl;
std::cout << "Enter a character: ";
std::cin >> cha;
std::cout << "ASCII Value of " << cha << " is " << int(cha) << std::endl;
if(ch == cha)
{
std::cout << "ASCII Value of " << cha << " is " << "equal to " << ch;
}
else
{
std::cout << ch << " is greater than " << cha << std::endl;
if(ch > cha)
std::cout << ch << " is greater than " << cha << std::endl;
else
std::cout << ch << " is less than " << cha;
}
return 0;
}
In this program, choices other than 0 to 4 are all invalid so the user is asked for his/her choice again and again. Then, as choice "1" is entered, the program would perform addition of two numbers. Therefore, it would proceed to ask for the two numbers (x and y) and then print out the calculation result.The user can then choose to perform another calculation. As long as the user does not choose the choice "5" to quit the program, the program would continue to do different calculations chosen by the user.I want to exit the program when choice == 5 . Yet it only returns me "Your choice is invalid, please enter again." Here is the code:
#include <iostream>
using namespace std;
int main()
{
int choice, x, y, z;
do
{
cout << "Math is easy!" << endl << "1. Perform addition" << endl << "2. Perform subtraction" << endl <<
"3. Perform multiplication" << endl << "4. Perform division" << endl << "5. Quit" << endl << "Please enter your choice[1 - 5]:" << endl;
cin >> choice;
while (choice < 1 || choice > 4)
{
cerr << "Your choice is invalid, please enter again.";
cin >> choice;
}
cout << "Please enter x:" << endl;
cin >> x;
cout << "Please enter y:" << endl;
cin >> y;
if (choice == 1)
{
z = x + y;
cout << x << "+" << y << "=" << z << endl;
}
if (choice == 2)
{
z = x - y;
cout << x << "-" << y << "=" << z << endl;
}
if (choice == 3)
{
z = x * y;
cout << x << "*" << y << "=" << z << endl;
}
if (choice == 4)
{
z = x / y;
cout << x << "+" << y << "=" << z << endl;
}
} while (choice != 5);
system("pause");
return 0;
}
How should I do?
Before while loop you can check if(choice == 5) and break;
if(choice == 5)// if choice == 5 break loop. or exit(0);
break;
while (choice < 1 || choice > 4)
{
cerr << "Your choice is invalid, please enter again.";
cin >> choice;
}
.
.
.
.
if (choice == 1)
{
z = x + y;
cout << x << "+" << y << "=" << z << endl;
}
else if (choice == 2)
{
//code
}
else if (choice == 3)
{
//code
}
Use if - elseif instead of using multiple if condition. you can also use switch.
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.