Post: My First Application (started C++ today)
02-26-2011, 05:45 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Hello guys, can you please give me some feedback on my app? It's a simple command prompt based thing that will calculate how much more money you need to buy something. Here it is:

    #include "stdafx.h"

#include <iostream>


int subtract(int x, int y)
{
return x - y;
}


int main()
{
using namespace std;
cout << "How much money does the desired item cost?";
int x;
cin >> x;
cout << "How much money do you have currently?";
int y;
cin >> y;
cout << "You need this much more: " << subtract(x, y) << endl;
return 0;
}


Note: This was done in Visual C++ 2010 Express.

How could I improve it? How could I code it to ask how many items are wanted, and make it calculate how much more is needed to purchase the 2-3 items.

Thanks!
03-12-2011, 07:15 PM #11
Originally posted by kiwimoosical View Post
Mine is like yours but shorter, faster, and better Happy


Yours may be more efficient BUT this is a guy who just started C++ not a person who knows how to do vectors and arrays. My way is noob-friendly.

---------- Post added at 03:15 PM ---------- Previous post was at 03:14 PM ----------

Originally posted by TheUberFail View Post
Explicit variable names do not speed up the application btw Happy!!! xD


Just makes it harder in the long run.
03-12-2011, 07:18 PM #12
Originally posted by MoBaTeY View Post
Yours may be more efficient BUT this is a guy who just started C++ not a person who knows how to do vectors and arrays. My way is noob-friendly.

---------- Post added at 03:15 PM ---------- Previous post was at 03:14 PM ----------



Just makes it harder in the long run.


Yes using explicit variable names makes it harder for others to see Whu-Teh-Heil is going on Smile
03-12-2011, 07:20 PM #13
Originally posted by TheUberFail View Post
Yes using explicit variable names makes it harder for others to see Whu-Teh-Heil is going on Smile


He could've at least organize it instead of 1-lining it.
03-12-2011, 07:22 PM #14
Originally posted by MoBaTeY View Post
He could've at least organize it instead of 1-lining it.


What you mean? he did organize it Smile
03-12-2011, 07:24 PM #15
Originally posted by TheUberFail View Post
What you mean? he did organize it Smile


It is organized for the people who know c++ but for the people who dont know whats going on, it can be simplified into much more lines so they can actually read it and see how it works. Then they can go to what the other guy did and see and learn how to combine it all into 1.
03-12-2011, 09:29 PM #16
kiwimoosical
Bounty hunter
Originally posted by TheUberFail View Post
Explicit variable names do not speed up the application btw Happy!!! xD


Ternary operation does. Look at the process in assembly, there is no branch rather than using an if statement where there is a branch which could cause a memory leak.

If you don't know what ternary operation is:
     cout << ((expression to be evaluated as boolean) ? "this is the true value as object" : "this is the false value as object") << endl;
03-12-2011, 09:47 PM #17
Originally posted by kiwimoosical View Post
Ternary operation does. Look at the process in assembly, there is no branch rather than using an if statement where there is a branch which could cause a memory leak.

If you don't know what ternary operation is:
     cout << ((expression to be evaluated as boolean) ? "this is the true value as object" : "this is the false value as object") << endl;


The compiler automatically optimizes it all anyway.

---------- Post added at 04:47 PM ---------- Previous post was at 04:46 PM ----------

Originally posted by MoBaTeY View Post
It is organized for the people who know c++ but for the people who dont know whats going on, it can be simplified into much more lines so they can actually read it and see how it works. Then they can go to what the other guy did and see and learn how to combine it all into 1.


Not sure what your talking about, no one has posted 1-line code here anyway?
03-12-2011, 09:49 PM #18
kiwimoosical
Bounty hunter
Originally posted by TheUberFail View Post
The compiler automatically optimizes it all anyway.

---------- Post added at 04:47 PM ---------- Previous post was at 04:46 PM ----------



Not sure what your talking about, no one has posted 1-line code here anyway?


Wrong. Ternary and If are compiled differently. Ternary is usually more efficient unless you want to start some sort of threaded process.
03-12-2011, 10:12 PM #19
Originally posted by kiwimoosical View Post
Wrong. Ternary and If are compiled differently. Ternary is usually more efficient unless you want to start some sort of threaded process.


What im confused?

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo