#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 (int a, int b, int c) // 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 (int a, int b, int c) // 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 << " /(6.1*5.0)" << endl; //how can I use variables instead of using quotes?
cout << "------" << endl;
cout << setprecision(2) << a/(b*c) << "\n" << endl;
system("PAUSE");
return 0;
}
I have used a repetitive layout that I was hoping would print 3 functions vertically so that decimals all line up. I cannot seem it to get to print without errors and think I don't understand the error outputs enough to make necessary changes. I don't know if I am redefining the variables properly or if I am putting them together properly (using {}).
Thanks to anyone who can help me get this to work.
Here is the output:
(7): error C2082: redefinition of formal parameter 'a'
(8): error C2082: redefinition of formal parameter 'b'
(9): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
(10): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
(21): error C2082: redefinition of formal parameter 'a'
(22): error C2082: redefinition of formal parameter 'b'
(23): error C2082: redefinition of formal parameter 'c'
(24): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
(25): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
(26): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
(34): error C2601: 'calculation' : local function definitions are illegal
(20): this line contains a '{' which has not yet been matched
(51): fatal error C1075: end of file found before the left brace '{'
How would I fix these errors?
You missed the closing brace after:
int calculate (int a, int b, int c) // print to console: (7.1*8.3)-2.2=56.73
{
....
....
cout << setprecision(2) << (a*b)-c << "\n" << endl;
}
^^^^
Since you have same named symbols in both functions the missing brace leads to the symbol names being used more than once violating the One Definition Rule and hence the redefinition error.
Also, note what #Ed.S rightly points out in comments.
Also, note the warning of type conversion you may want to consider that in your program logic.
First of all:
int main(int, int) - very non-standard program entry point
Next one :
int main (int a, int b) // print to console: 3.0*5.0=15.00
{
double a;
double b;
You are redefining formal parameters a and b
That's it, this is what your compiler is saying:
(7): error C2082: redefinition of formal parameter 'a'
(8): error C2082: redefinition of formal parameter 'b'
.
int calculate (int a, int b, int c) // print to console: (7.1*8.3)-2.2=56.73
{
double a;
double b;
double c;
You have same local variables and formal parameter names, I don't think this is what you are expecting.
And last one:
You are missing closing bracket "}" at the end of calculate function.
#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;
}
You're missing the } after your int calculate function. Although unrelated, you also don't have to call fixed and setprecision multiple times.
Related
I'm trying to make a C++ Acidity/Base Universal Calculator. Upon trying to finalize my code, I stump upon 5 error(s) below;
main.cpp: In function 'int main()':
main.cpp:62:36: error: invalid operands of types 'double' and 'const char [5]' to binary 'operator<<'
cout << "[H+]=" << 10^(W-X) << "*10^" << W << '\n';
^
main.cpp:63:34: error: invalid operands of types 'int' and 'double' to binary 'operator^'
cout << "[OH-]=" << 1/(10^(W-X)) << "*10^" << (-14)-W << '\n';
^
main.cpp:77:33: error: invalid operands of types 'int' and 'double' to binary 'operator^'
cout << "[H+]=" << 1/(10^(U-V)) << "*10^" << (-14)-U << '\n';
^
main.cpp:78:37: error: invalid operands of types 'double' and 'const char [5]' to binary 'operator<<'
cout << "[OH-]=" << 10^(U-V) << "*10^" << U << '\n';
^
D:\EvaxHybrid\Mywork\Cpp\ChempHpOH\Makefile.win:28: recipe for target 'main.o' failed
mingw32-make.exe: *** [main.o] Error 1
I've tried implementing Solution 1 which isn't inline and would make the code too complex to read , Solution 2 which isn't inline and not the same problem (I didn't use new). If there's no other choice, anyone could comment to me about that and I'll do a function related approach.
Here's the code (main.cpp);
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <conio.h>
#include <cmath>
using namespace std;
int main() {
cout << "Choose Start point..." << '\n';
cout << "1. [H+]\n2. [OH-]\n3. pH\n4. pOH\n";
int choice;
cin >> choice;
system ("cls");
switch(choice)
{
case 1:
cout << "Convert [H+] to Scientific Notation of A*10^B and input A,B\n";
system ("pause");
cout << '\n';
double A,B;
cout << "A:";
cin >> A;
cout << '\n';
cout << "B:";
cin >> B;
cout << '\n';
cout << "[H+]=" << A << "*10^" << B << '\n';
cout << "[OH-]=" << 1/A << "*10^" << (-14)-B << '\n';
cout << "[pH]=" << (-log10(A)-B) << '\n';
cout << "[pOH]=" << 14-(-log10(A)-B) << '\n';
break;
case 2:
cout << "Convert [OH-] to Scientific Notation of Z*10^Y and input Z,Y\n";
system ("pause");
cout << '\n';
double Z,Y;
cout << "Z:";
cin >> Z;
cout << '\n';
cout << "Y:";
cin >> Y;
cout << '\n';
cout << "[H+]=" << 1/Z << "*10^" << (-14)-Y << '\n';
cout << "[OH-]=" << Z << "*10^" << Y << '\n';
cout << "[pH]=" << 14-(-log10(Z)-Y) << '\n';
cout << "[pOH]=" << (-log10(Z)-Y) << '\n';
break;
case 3:
cout << "Input pH as X\n";
system ("pause");
cout << '\n';
double X;
cout << "X:";
cin >> X;
double W;
W = -ceil(X);
cout << '\n';
cout << "[H+]=" << 10^(W-X) << "*10^" << W << '\n';
cout << "[OH-]=" << 1/(10^(W-X)) << "*10^" << (-14)-W << '\n';
cout << "[pH]=" << X << '\n';
cout << "[pOH]=" << 14-X << '\n';
break;
case 4:
cout << "Input pOH as V\n";
system ("pause");
cout << '\n';
double V;
cout << "V:";
cin >> V;
double U;
U = -ceil(V);
cout << '\n';
cout << "[H+]=" << 1/(10^(U-V)) << "*10^" << (-14)-U << '\n';
cout << "[OH-]=" << 10^(U-V) << "*10^" << U << '\n';
cout << "[pH]=" << 14-V << '\n';
cout << "[pOH]=" << V << '\n';
break;
}
}
Operator << has precedence over operator ^.
cout << "[H+]=" << 10^(W-X) << "*10^" << W << '\n';
is read as
(cout << "[H+]=" << 10) ^ ((W-X) << "*10^" << W << '\n');
Put parenthesis:
cout << "[H+]=" << (10^(W-X)) << "*10^" << W << '\n';
In this program I got the error
[Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'numcall')
I can't understand how to get rid of it!!
#include<iostream>
using namespace::std;
class numcall
{
int a,b;
public:
numcall(int c,int d)
{
c = a;
c = b;
cout<<"Your First num is " << c << endl << "Your Second num is "<< d << endl;
cout << "You are in PARAMETER CONSTRUCTOR";
}
numcall(int u)
{
u = a;
b = 0;
cout << "Your First num is " << u << endl << "Your Second num is " << b << endl;
cout << "You are in PARAMETER CONSTRUCTOR";
}
numcall()
{
}
};
int main(void)
{
numcall x = numcall();
numcall y = numcall(3,4);
numcall z = numcall(3);
cout << x << endl << endl << y << endl << endl << z << endl;
}
You haven't defined the << operator for your class numcall, so the compiler doesn't know how to apply it.
So define it.
You need to define a friend method for the << stream operator, otherwise he doesn't know what to print.
friend ostream &operator<<(ostream &os, const numcall &numcall1) {
os << "a: " << numcall1.a << " b: " << numcall1.b;
return os;
}
This is just an example of implementation.
By the way there are other mistakes: c=a means assign a to c, you want to do the other way around. Welcome to the programming world mate ;)
I have been started C++ lessons in my University since 3 weeks, so I'm quite beginner.
I have 4 codes to find the errors and fix them, 2 of them are done, but i couldn't figure out why did not the other 2 get compiled.
There's the first code;
#include <cstring>
#include <iostream>
#include <climits>
#include <cfloat>
#include <cmath>
using std::cout;
using std::endl;
template<class T>
void to_bin(T v)
{
union
{
T value;
unsigned char bytes[sizeof(T)];
};
memset(&bytes, 0, sizeof(T));
value=v;
// assumes little endian machine
for (size_t i = sizeof(T);i>0;i--)
{
unsigned char pot=128;
for (int j = 7;j>=0;j--,pot/=2)
if (bytes[i-1]&pot)
cout << "1";
else
cout << "0";
cout << " ";
}
cout << endl;
}
int main() {
cout <<" Binaerdarstellungen von -2, -1, 0, 42, INT_MAX und INT_MAX+1 als Int: " << endl;
int p_i = (-2), q_i=-1,r_i=0,s_i=42,t_i=INT_MAX,u_i=INT_MAX+1 ;
cout << "Fuer -2: ";
to_bin(p_i);
cout << endl;
cout << "Fuer -1: ";
to_bin(q_i);
cout << endl;
cout << "Fuer 0: ";
to_bin(r_i);
cout << endl;
cout << "Fuer 42: ";
to_bin(s_i);
cout << endl;
cout << "Fuer INT_MAX: ";
to_bin(t_i);
cout << "Fuer INT_MAX+1: ";
to_bin(u_i);
cout << endl;
cout << endl << "Binaerdarstellungen von -2.0, 1.0, 0.0, 42.0, FLT_MAX, FLT_MAX+1 als float: " << endl;
float p_f = -2.0, q_f= -1.0,r_f=0.0,s_f=42.0,t_f=FLT_MAX,u_f=FLT_MAX+1 ;
cout << "Fuer -2.0: ";
to_bin(p_f);
cout << endl;
cout << "Fuer -1.0: ";
to_bin(q_f);
cout << endl;
cout << "Fuer 0.0: ";
to_bin(r_f);
cout << endl;
cout << "Fuer 42.0: ";
to_bin(s_f);
cout << endl;
cout << "Fuer FLT_MAX: ";
to_bin(t_f);
cout << endl;
cout << "Fuer FLT_MAX+1: ";
to_bin(u_f);
cout << endl;
cout << endl << "Binaerdarstellungen von -2.0, 1.0, 0.0, 42.0, DBL_MAX, DBL_MAX+1 als double: " << endl;
double p_d = -2.0, q_d=-1.0,r_d=0.0,s_d=42.0,t_d=DBL_MAX,u_d=DBL_MAX+1 ;
cout << "Fuer -2.0: ";
to_bin(p_d);
cout << endl;
cout << "Fuer -1.0: ";
to_bin(q_d);
cout << endl;
cout << "Fuer 0.0: ";
to_bin(r_d);
cout << endl;
cout << "Fuer 42.0: ";
to_bin(s_d);
cout << endl;
cout << "Fuer DBL_MAX: ";
to_bin(t_d);
cout << endl;
cout << "Fuer DBL_MAX+1: ";
to_bin(u_d);
cout << endl;
cout << endl << "Groesse von Integer Variablen: ";
cout << sizeof(p_i) << endl;
cout << "Groesse von Gleitkomma Variablen mit einfacher Genauigkeit: ";
cout << sizeof(p_f) << endl;
cout << "Groesse von Gleitkomma Variablen mit doppelter Genauigkeit: ";
cout << sizeof(p_d) << endl;
return 0;}
and so the other one;
#include <iostream>
#include <cstring>
using namespace std;
template<class T>
void to_bin(T v)
{
using std::cout;
using std::endl;
union
{
T value;
unsigned char bytes[sizeof(T)];
};
memset(&bytes, 0, sizeof(T));
value=v;
// assumes little endian machine
for (size_t i = sizeof(T);i>0;i--)
{
unsigned char pot=128;
for (int j = 7;j>=0;j--,pot/=2)
if (bytes[i-1]&pot)
cout << "1";
else
cout << "0";
cout << " ";
}
cout << endl;
}
int main() {
long unsigned int stack_field;
long unsigned int *heap_field = new long unsigned int;
short unsigned int *stack_pointer = (short unsigned int*)&stack_field;
short unsigned int *heap_pointer = (short unsigned int*)heap_field;
stack_field = 0;
*heap_field = 0;
*stack_pointer = 1;
*heap_pointer = 1;
stack_pointer = stack_pointer + 1;
heap_pointer = heap_pointer + 1;
to_bin(stack_field);
to_bin(*heap_field);
to_bin(*stack_pointer);
to_bin(*heap_pointer);
*stack_pointer = 1;
*heap_pointer = 1;
to_bin(stack_field);
to_bin(*heap_field);
to_bin(*stack_pointer);
to_bin(*heap_pointer);
// HIER EDITIEREN
cout << "stack_field " << sizeof(stack_field) <<" " << stack_field <<" "<< &stack_field << endl;
cout << "heap_field " << sizeof(heap_field) <<" " << *heap_field <<" "<< &heap_field << endl;
cout << "stack_pointer " << sizeof(stack_pointer) <<" " << *stack_pointer <<" "<< &stack_pointer << endl;
cout << "heap_pointer " << sizeof(heap_pointer) <<" " << *heap_pointer <<" "<< &heap_pointer << endl;
delete heap_field;
return 0;
}
What is the thing, that i'm doing wrong?
I only have 3 more hours to finish and upload it.
Thanks...
UPDATE
Errors of first code :
x#cluster:~/x/cppfiles[534]$ g++ -Wall -Werror -o aufgabe1 aufgabe1.cpp
cc1plus: warnings being treated as errors
aufgabe1.cpp: In function 'int main()':
aufgabe1.cpp:41: error: integer overflow in expression
aufgabe1.cpp: In function 'void to_bin(T) [with T = int]':
aufgabe1.cpp:44: instantiated from here
aufgabe1.cpp:17: error: unused variable 'value'
aufgabe1.cpp:17: error: unused variable 'bytes'
aufgabe1.cpp: In function 'void to_bin(T) [with T = float]':
aufgabe1.cpp:70: instantiated from here
aufgabe1.cpp:17: error: unused variable 'value'
aufgabe1.cpp:17: error: unused variable 'bytes'
aufgabe1.cpp: In function 'void to_bin(T) [with T = double]':
aufgabe1.cpp:97: instantiated from here
aufgabe1.cpp:17: error: unused variable 'value'
aufgabe1.cpp:17: error: unused variable 'bytes'
And errors of the second code:
x#cluster:~/x/cppfiles[534]$ g++ -Wall -Werror -o aufgabe4 aufgabe4.cpp
cc1plus: warnings being treated as errors
aufgabe4.cpp: In function 'void to_bin(T) [with T = long unsigned int]':
aufgabe4.cpp:48: instantiated from here
aufgabe4.cpp:15: error: unused variable 'value'
aufgabe4.cpp:15: error: unused variable 'bytes'
aufgabe4.cpp: In function 'void to_bin(T) [with T = short unsigned int]':
aufgabe4.cpp:50: instantiated from here
aufgabe4.cpp:15: error: unused variable 'value'
aufgabe4.cpp:15: error: unused variable 'bytes'
You probably get those eroors because of the -Wall (make gcc display "many" warnings) and -Werror (treat all warnings as errors).
So your code may compile fine if no special flags are given, but here you force adding warning and failing on them.
Fixing the code is then necessary:
for error: unused variable 'value' this is quite simple, just remove the useless variables.
For the overflow, check the value in order not to overflow (fix the code); my compiler points int u_i=INT_MAX+1 which is obviously overrflowing (you explicitly add 1 to the maximum value)
Note: adding -Wall -Werror is a good idea (IMHO) as it generally points coding errors or things that would improve your code.
there. I'm self learning C++ out of "C++ without fear". There is an exercise dealing with the GCD of 2 numbers that asks to print "GCD(a,b) =>" at each step in the proceedure. I was able to get this working:
int gcd (int a, int b);
int main() {
int i,j;
cout << "Enter the first integer" << endl;
cin >> i;
cout << "Enter the second integer" << endl;
cin >> j;
int k = gcd(i,j);
cout << "The GCD is " << k << endl;
system("PAUSE");
return 0;
}
int gcd (int a, int b){
if(b==0){
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " <<endl;
return a;
}
else {
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
return gcd(b,a%b);
}
}
I was just wondering if there is a nicer way to go about printing each step of finding the GCD. That is, is there a "nicer" way to write this part of the code:
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
? Thanks in advance.
You can do something like:
cout << "GCF(" << a << ',' << b << ") =>" << endl;
It is not C++ but you could use the C way of printing it which in my opinion looks better in this situation because there are far fewer stream operators, << in the way.
#include <cstdio>
printf("GCF(%d, %d) =>\n", a, b);
But this is a C way of doing things... You could use something like boost::format as is mentioned in this SO answer.
try this one
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
cout << 6+2 <<"\n" << 6-2;
}
I have overloaded a function fn as fn(int,char) & fn(int&,char&) as shown below:
#include <iostream>
using namespace std;
void fn(int a, char c);
void fn(int& a, char& c);
int main()
{
int a=10;
char c= 'c';
cout << "Inside main()" << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
static_cast<void(*) (int&, char&)> (fn)(a, c);
return 0;
}
void fn(int a, char c)
{
int tempInt;
char tempChar;
cout << "\n\nInside Call By Value Function " << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
cout << hex << "&tempInt : " << &tempInt << endl;
cout << hex << "&tempChar : " << (int *)&tempChar << endl;
}
void fn(int& a, char& c)
{
cout << "\n\nInside Call By Reference Function " << endl;
cout << hex << "*a : " << &a << endl;
cout << hex << "*c : " << (int*) &c << endl;
}
The resolution for call to fn(int,char) or fn(int&,char&) is made through cast static_cast<void(*) (int&, char&)> (fn)(a, c);
It gives output:
$ ./Overloading
Inside main()
&a : 0x22ac5c
&c : 0x22ac5b
Inside Call By Reference Function
*a : 0x22ac5c
*c : 0x22ac5b
Now when I put this in a class as below:
#include <iostream>
using namespace std;
class Test{
public:
void fn(int a, char c);
void fn(int& a, char& c);
};
int main()
{
int a=10;
char c= 'c';
Test T();
cout << "Inside main()" << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
static_cast<void(*) (int&, char&)> (T.fn)(a, c);
return 0;
}
void Test::fn(int a, char c)
{
int tempInt;
char tempChar;
cout << "\n\nInside Call By Value Function " << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
cout << hex << "&tempInt : " << &tempInt << endl;
cout << hex << "&tempChar : " << (int *)&tempChar << endl;
}
void Test::fn(int& a, char& c)
{
cout << "\n\nInside Call By Reference Function " << endl;
cout << hex << "*a : " << &a << endl;
cout << hex << "*c : " << (int*) &c << endl;
}
I get below error:
$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:42: error: request for member ‘fn’ in ‘T’, which is of non-class type ‘Test()’
How do I resolve this?
How to make a proper call for T's fn(int&,char&)
I guess in my code the expression static_cast<void(*) (int&, char&)> (T.fn)(a, c); is incorrect.
Please help.
Thanks
EDIT:
My mistake
editing Test T() to Test T;
gives error
$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:44: error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘void (*)(int&, char&)’
First: T is no variable, T is function, that returns Test and receive nothing.
Second: function-pointer is not member-function-pointer. You should use this syntax
typedef void (Test::*function)(int&, char&);
function f = &Test::fn;
(T.*f)(a, c);
See error: request for member '..' in '..' which is of non-class type: the problem is with Test T();, try ommiting the parentheses.
When you do Test T();, you're saying that T is a function with return type Test. However no such thing exists in your code.
The solution is:
Test T;