Post: C++ Basic Calculation
08-02-2013, 01:22 AM #1
Pichu
RIP PICHU.
(adsbygoogle = window.adsbygoogle || []).push({}); Yea, I'm still learning C++. I'm getting the hang of it and have just started back up in my book the other day. Basically dealing with a lot of streaming right now. Wrote this up to see where I am from memory. Please note; I haven't entered looping or any of that crap for C++ but I figured seeing C# being partially derived from C++; it should work. :P

    #include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main()
{
double num1 = -1, num2 = -1, num3 = -1;
while(num1 < 0 || num1 > 10)
{
cout << "Hey there! I'd like you to choose a number between 1 & 10" << endl;
cin >> num1;
if (num1 < 0 || num1 > 10)
cout << "The value you have input was not within the scope specified." << endl;
}
cout << "The number you have chosen is: " << num1 << endl;


while (num2 < 0 || num2 > 10)
{
cout << "This time I want you to choose another number between 1 & 10" << endl;
cin >> num2;
if (num2 < 0 || num2 > 10)
cout << "The value you have input was not within the scope specified." << endl;
}
cout << "The number you have chosen is: " << num2 << endl;


while (num3 < 0 || num3 > 50)
{
cout << "This time I want you to choose another number between 1 & 50" << endl;
cin >> num3;
if (num3 < 0 || num3 > 50)
cout << "The value you have input was not within the scope specified." << endl;
}
cout << "The number you have chosen is: " << num3 << endl;


cout << num1 * num2 / num3 << endl;
cout << "We have calculated the values." << endl;
cin;
return 0;
}
08-07-2013, 12:03 AM #2
Originally posted by Pichu View Post
Yea, I'm still learning C++. I'm getting the hang of it and have just started back up in my book the other day. Basically dealing with a lot of streaming right now. Wrote this up to see where I am from memory. Please note; I haven't entered looping or any of that crap for C++ but I figured seeing C# being partially derived from C++; it should work. :P

    #include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main()
{
double num1 = -1, num2 = -1, num3 = -1;
while(num1 < 0 || num1 > 10)
{
cout << "Hey there! I'd like you to choose a number between 1 & 10" << endl;
cin >> num1;
if (num1 < 0 || num1 > 10)
cout << "The value you have input was not within the scope specified." << endl;
}
cout << "The number you have chosen is: " << num1 << endl;


while (num2 < 0 || num2 > 10)
{
cout << "This time I want you to choose another number between 1 & 10" << endl;
cin >> num2;
if (num2 < 0 || num2 > 10)
cout << "The value you have input was not within the scope specified." << endl;
}
cout << "The number you have chosen is: " << num2 << endl;


while (num3 < 0 || num3 > 50)
{
cout << "This time I want you to choose another number between 1 & 50" << endl;
cin >> num3;
if (num3 < 0 || num3 > 50)
cout << "The value you have input was not within the scope specified." << endl;
}
cout << "The number you have chosen is: " << num3 << endl;


cout << num1 * num2 / num3 << endl;
cout << "We have calculated the values." << endl;
cin;
return 0;
}


Simpler
    
using namespace std;


     
// Instead of cout << "" << endl;
printf("contents");
08-07-2013, 01:08 AM #3
Pichu
RIP PICHU.
Originally posted by TheUnexpected View Post
Simpler
    
using namespace std;


     
// Instead of cout << "" << endl;
printf("contents");


It's what I am being taught so far in C++. I'm sure what you had written is what I will later learn to use as right now, from what I get from what occurs, since endl flushes the stream; if I was to use this truly in a loop, because I am clearing the stream; I'd end up with a slight performance hit. Overall, I could have used cout << " \n" to achieve the same thing you achieved.

The only thing that I notice between printf and cout is that printf lacks streaming capabilities as well as it lacking type safety.

From what I understand at this point, cout is more proper to use in terms of consistency but once I further progress in my book; if I come to the point where I learn the true full values of each, then I will make my decision.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo