Distac_221
your if statement should be if(strlen(str)==0)
thank you for your help, but when i implement your code and test it without any input it just past away
Sal
That's fine
Harleen
Why not g++ ?
I will try with that. Thank you. strangely, in my header file, it says that #include <string> file not found. :/ But the same code line is working in .cpp extension.
Anonymous
#include<iostream> using namespace std; class calculator { public: int sum , multiply , divide , sub ; void Answers(int sum , int sub , int multiply , int divide){ cout<< " The sum of a and b = " << sum << endl; cout<< " The sub of a and b = " << sub << endl; cout<< " The multiplication of a and b = " << multiply << endl; cout<< " The divition of a and b = " << divide << endl;} int add(int a , int b ){ int sum ; sum = a + b ; return sum ; } int Mul(int a , int b ){ int multiply; multiply = a * b ; return multiply ; } int dvd(int a , int b ){ int divide ; divide = a / b ; return divide ;} int subtract(int a , int b ){ int sub ; sub = a - b ; return sub ;} }; int main() { int sum , sub , multiply,divide,a = 5 ,b = 2 ; calculator maths; maths.Answers(sum , sub , multiply,divide); }
Anonymous
why is it giving wrong value
Pavel
Didnt get it
What output of the program do you expect?
Anonymous
I want a = 5 , b = 3 got sum , sub , multiply , and divide and give me answers
Pavel
I want a = 5 , b = 3 got sum , sub , multiply , and divide and give me answers
Let's investigate one variable sum. In main you have this variable not initialized (so it has some garbage value), then you pass it to your Answers method and then you just print this garbage value. For other 3 variables happens the same. And you don't do anything with a and b.
Pavel
So you set a to 5 and then not using it, same for b
Pavel
I used sum and 3 other because that it is asking for in object of class
You need to try to understand what you're doing in each line, starting from main.
Pavel
Try to use a debugger to investigate the program execution line by line
Pavel
And also note that you have arguments, local variables and member variables named the same but they are different variables.
Anonymous
😁😁😁
Anonymous
In which lines i have mistake
Anonymous
Still not working
Pavel
In which lines i have mistake
I would guess you would need to run add, Mul, dvd and subtract functions on maths object, and assign the result to appropriate variables before calling Answers. Then the mistake is in main function. And if that's the case you probably don't need member variables in your class (because it seems that you are not using them)
Anonymous
#include<iostream> using namespace std; int main() { int a, b, c, d, e; int total; float average; cout <<"Input first number : "; cin >> a; cout <<"Input second number : "; cin >> b; cout <<"Input third number : "; cin >> c; cout <<"Input 4th number : "; cin >> d; cout <<"Input last number : "; cin >> e; total = a+b+c+d+e; average = total/5; cout <<"Total is " <<total; cout<<endl; cout <<"Average is " <<average << endl; return 0; }
Pavel
do you mean float=total;
total is int, you divide it by int, the result will be int and then converted to float. So 5/2 => 2 and then converted to float being 2.0f; If you divide a float or/and by float, then it will be a floating point division: 5/2.0f => 2.5f or 5.0f/2 => 2.5f
Anonymous
Thank you My friend
Miss Bethany
@B2nwd
Anonymous
1/Write a program that asks the user to type 5 integers and writes the average of the 5 integers. 2/Write a program that asks the user to type 2 integers A and B and exchange the value of A and B. 3/Write a program that asks the user to type the price without tax of one kilogram of tomatoes,the number of kilograms you want to buy and the tax in percent units. The program must write the total price including taxes.
Anonymous
Help reduce
Anonymous
Get it now
Distac_221
your if statement should be if(strlen(str)==0)
i was implement your code but the printf of if statement was still not shown
coal
what's the purpose of empty templates? template<> int myfunction() { return 10; } i can't seem to find any good explanation online
100$ website
[Error] 'else' without a previous 'if'
100$ website
coal
you have an else without a previous if
coal
glad to help
100$ website
you have an else without a previous if
#include<iostream> using namespace std; int main() { int number; char array1[][10]={"one","two","three","four","five","six","seven","eight","nine","ten"}; char array2[][10]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; char array3[][10]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty","hundred"}; cin>>number; { if(number<0); cout<<"the number is negative number"; else if(number>0 && number<=10) cout<<"the number is "<<array1[number-1]; } return 0; }
100$ website
i have if yet it gives that error so am cnfused
coal
if(number<0);
100$ website
got it thank you :)
coal
got it thank you :)
btw its recommendable to always use braces in conditionals
coal
they denote the scope explicitly and avoid confusions like that
coal
plus makes the code more readable
Distac_221
why its always give "please input your name" even if i gave input https://pastebin.com/asz0rrp9
hey @coalio can i ask you. when i input my name with nothing (just enter) it just pass away with Hello,
coal
coal
"If the string len is not 0 then print"
coal
lol, it asks you only if you input something
Distac_221
if(strlen(str) !=0){ printf("Please input your name\n"); }
thank you but when i use that it still give same result
coal
"If the string len is not 0 then print"
this is what these lines of code do
coal
"if the string len is above 0, then print"
coal
strlen("distac_221") != 0 is true because distac_221 is 10 characters long, and 10 != 0
coal
!= means not equal
coal
you're basically asking to always receive empty input
Distac_221
so the right was if(strlen(str) == 0){ printf("Please input your name\n") }
Distac_221
but when i implement that it gave same result
100$ website
btw its recommendable to always use braces in conditionals
#include<iostream> using namespace std; int main() { int number,a1,a2; char array1[][10]={"one","two","three","four","five","six","seven","eight","nine","ten"}; char array2[][10]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; char array3[][10]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty","hundred"}; cin>>number; { if(number<0) cout<<"the number is negative number"; else if(number>0 && number<=10) cout<<"the number is "<<array1[number-1]; else if(number>10 && number<20) cout<<"the number is "<<array2[number-11]; else if(number>=20 && number<=100) a1=number/10; a2=number%10; cout<<"the number is "<<array3[a1-2]<<" "<<array1[a2-1]; } return 0; }
coal
but when i implement that it gave same result
paste a return 1; after the printf
100$ website
paste a return 1; after the printf
#include<iostream> using namespace std; int main() { int number,a1,a2; char array1[][10]={"one","two","three","four","five","six","seven","eight","nine","ten"}; char array2[][10]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; char array3[][10]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty","hundred"}; cin>>number; { if(number<0) cout<<"the number is negative number"; else if(number>0 && number<=10) cout<<"the number is "<<array1[number-1]; else if(number>10 && number<20) cout<<"the number is "<<array2[number-11]; else if(number>=20 && number<=100) a1=number/10; a2=number%10; cout<<"the number is "<<array3[a1-2]<<" "<<array1[a2-1]; } return 1; }
100$ website
why the d after thirty
coal
thirty d lmao
coal
why the d after thirty
probably some character left in the array
100$ website
nope
coal
array1[a2-1];
coal
what is this supposed to contain if the first array already has the entire number in it
100$ website
100$ website
what is this supposed to contain if the first array already has the entire number in it
any solution to it? if i remove it i wont get the right answer in other numbers
Anonymous
can I ask java problem here?