I thought java was hard. Whats with all the ">>" why do u need them?
As a few of you already know, I've started to get my feet wet in C++, and this is what I've got so far:
Code:#include "stdafx.h" #include <iostream> using namespace std; int x; int y; int main() { cout << "Let's calculate an average!" << endl << endl; // Simple title, going to figure out colors next cout << "Input your first number: " << endl << endl; // Asks for input, no actual search for an input cin >> x; // Grabs users input, stores it in x cout << endl; // Line skip to make it look pretty cout << "Your first number is: " << x << endl << endl; // Spits back x value, because why not cout << "Input your second number: " << endl << endl; // Asks for input, no actual search for an input cin >> y; // Grabs users input, stores it in y cout << endl; // Line skip to make it look pretty cout << "Your second number is: " << y << endl << endl; // Spits back y value, because why not system("cls"); // Clear that shit cout << "Your average is: " << (x + y) / 2 << endl << endl; // Outputs the average, super simple. return 0; // Ain't returnin' shit. }
I thought java was hard. Whats with all the ">>" why do u need them?
<< and >> are usually for bit shifting, however in the context of streams(input/output) they're overloaded to pipe text into and out of the stream.
Also, a note for OP; You're working with integers, try 1 and 2 as input, this will probably result in 1 where you'd want it to return 1.5. See if you can fix that
Last edited by Hell_Demon; 05-24-2017 at 03:16 AM.
Ah we-a blaze the fyah, make it bun dem!
Zombie (05-24-2017)
Hell_Demon (05-26-2017),Zombie (05-29-2017)
Here's another app you can try out:
You can run it here: https://cpp.sh/8rkgCode:/* For std::cin and std::cout */ #include <iostream> /* For std::numeric_limits<> */ #include <limits> /* For std::unique_ptr<> and std::make_unique<> */ #include <memory> int main(int argc, char** argv) { /* Define the scalar values and initialize them. */ auto sum = 0.0, average = 0.0, max = 0.0, min = std::numeric_limits<double>::max(); /* Holds the maximum number of numbers the user wishes to calculate for. */ auto maxNumbers = 0; /* Request the input from the user. */ std::cout << "Enter the number of numbers you'd like to calculate the sum, average and min/max of: "; std::cin >> maxNumbers; /* Prompt the user to enter the numbers. */ std::cout << "Please enter " << maxNumbers << " numbers:" << std::endl; /* Allocate N numbers as an array of smart pointers. */ auto numbers = std::make_unique< long double[] >( maxNumbers ); /* Do the following till we have all numbers. */ for(auto i = 0; i < maxNumbers; i++) { /* Get a number. */ std::cin >> numbers[ i ]; /* Add it to the sum, thus far. */ sum += numbers[ i ]; /* Check if the entered number exceeds the maximum stored. */ if( numbers[ i ] > max ) /* Update the maximum. */ max = numbers[ i ]; /* Check if the entered number exceeds the maximum stored. */ if( numbers[ i ] < min ) /* Update the maximum. */ min = numbers[ i ]; /* Friendly user output to indicate they may enter another number. */ std::cout << "You entered: " << numbers[ i ] << std::endl; } /* Calculate the average. */ average = sum / static_cast<double>(maxNumbers); /* Output the results. */ std::cout << "Sum: " << sum << std::endl; std::cout << "Average: " << average << std::endl; std::cout << "Minimum: " << min << std::endl; std::cout << "Maximum: " << max << std::endl; return 0; }
:O I saw arrays and noped out haha, I've still got some reading to do on this C++ Primer book but once I do get through reading about arrays, I'll come back to your code and see if I can make sense of it. I really appreciated your contribution though! I'm sure it'll be great practice when I get there.
Hitokiri~ (05-26-2017)
@ZombieCake float instead of int for getting 1.5
prohacku (06-15-2017),thxtesting (06-15-2017),thxtesting2 (06-15-2017),Zombie (05-29-2017)
Zombie, try this.(and Yes its my code)
Ask me if you don't understand, Its pretty basic code with functions and loops.
You can also find power by using loop instead of using "math.h" library. Try that!
Code:#include "iostream" #include "conio.h" #include "math.h" using namespace std; float areaC(float); float areaR(float, float); float add(float, float); float sub(float, float); float mul(float, float); float div(float, float); float power(float, float); float factorial(float number); void fibonacci(); void average(); void mean(); void menu(); int main() { float x = 0, y = 0, result = 0; int choice; menu(); choice=getch(); switch (choice) { case 49: cout << "Enter number 1 and 2: "; cin >> x >> y; result = add(x, y); cout << "Addition result of both numbers is: " << result; break; case 50: cout << "Enter number 1 and 2: "; cin >> x >> y; result = sub(x, y); cout << "Subtraction result of both numbers is: " << result; break; case 51: cout << "Enter number 1 and 2: "; cin >> x >> y; result = mul(x, y); cout << "Multiplication result of both numbers is: " << result; break; case 52: cout << "Enter number to find its square: "; cin >> x; result = power(x, 2); cout << "Square of number \""<<x<<"\" is: " << result; break; case 53: cout << "Enter number to find its cube: "; cin >> x; result = power(x, 3); cout << "Cube of number \""<<x<<"\" is: " << result; break; case 54: cout<<"Enter the number to find its factorial: "<<endl; cin>>x; cout<<"Factorial of the number is "<<factorial(x)<<endl; break; case 55: fibonacci(); break; case 56: average(); break; case 57: mean(); break; default: cout << "wrong choice"; } _getch(); return 0; } float add(float x, float y) { return x + y; } float sub(float x, float y) { return x - y; } float mul(float x, float y) { return x * y; } float power(float x, float y) { return pow(x, y); } float factorial(float number) { int temp=number; for(int i=number-1;i>=2;i--) { temp=temp*i; } return temp; } void fibonacci() { { int n, t1 = 0, t2 = 1, next = 0; cout << "Enter the number of terms in fibonacci series: "; cin >> n; cout<<endl; for (int i = 1; i <= n; ++i) { if(i == 1) { cout << " " << t1; continue; } if(i == 2) { cout <<" "<< t2 << " "; continue; } next = t1 + t2; t1 = t2; t2 = next; cout << next << " "; } } } void average() { int j=0,x=0,result=0;; cout<<"Enter total amount of numbers to find average of: "<<endl; cin>>j; for(int i=0;i<j;i++) { cout<<"Enter number "<<i+1<<":"; cin>>x; result=x+result; } cout<<"Average result of numbers is "<<result/j; } void mean() //or called midrange otherwise mean is same as average^^ //Mean or Midrange is the smallest number + largest number divided by 2. { int x=0,result=0; cout<<"Enter the integers to find their mean, enter 99 to end."<<endl; cin>>x; int max=x; int min=x; while(true) { cin>>x; if(x==99){ break; } else if(x>max){ max=x; } else if(x<min){ min=x; } } result=max+min; cout<<"Mean or Midrange result of the numbers is "<<result/2; } void menu() { cout << "1) Press 1 for Addition .\n2) Press 2 for Subtraction \n"<< "3) Press 3 for Multiplication.\n4) Press 4 for Square.\n5) Press 5 for Cube.\n6) Press 6 for Factorial."<< "\n7) Press 7 for Fibonacci series.\n8) Press 8 for Average.\n9) Press 9 for Mean."<< endl<<endl<<endl; }