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, 10:18 PM #20
kiwimoosical
Bounty hunter
Originally posted by TheUberFail View Post
What im confused?


C++ If statement compiled assembly decompiled:
    int main() 
{
int x = 0;
if (x == 0)
{
x = 1;
}
else
{
x = 2;
}
return 0;
}

You must login or register to view this content.

C++ Ternary operation compiled assembly decompiled:
    int main() 
{
int x = 0;
x = x == 0 ? 1 : 2;
return 0;
}

You must login or register to view this content.

The if statement branches, the ternary does not. Therefore no memory leak possible unless your doing some other branching. But ternary is safer, and more efficient, and faster.
03-12-2011, 10:47 PM #21
Originally posted by kiwimoosical View Post
C++ If statement compiled assembly decompiled:
    int main() 
{
int x = 0;
if (x == 0)
{
x = 1;
}
else
{
x = 2;
}
return 0;
}

You must login or register to view this content.

C++ Ternary operation compiled assembly decompiled:
    int main() 
{
int x = 0;
x = x == 0 ? 1 : 2;
return 0;
}

You must login or register to view this content.

The if statement branches, the ternary does not. Therefore no memory leak possible unless your doing some other branching. But ternary is safer, and more efficient, and faster.


Ok, that is very cool, and i kind of understand that, but what does it have to do with how long a variable name is *Scratches head*
03-12-2011, 10:50 PM #22
kiwimoosical
Bounty hunter
Originally posted by TheUberFail View Post
Ok, that is very cool, and i kind of understand that, but what does it have to do with how long a variable name is *Scratches head*


It doesn't lol, I was just showing how mine was teh best :P
03-12-2011, 11:06 PM #23
Originally posted by kiwimoosical View Post
It doesn't lol, I was just showing how mine was teh best :P


Yea i know that, i use them for functions

#define isFive(x) (x == 5) ? 1 : 0

better than using an entire function for it.
03-12-2011, 11:08 PM #24
kiwimoosical
Bounty hunter
Originally posted by TheUberFail View Post
Yea i know that, i use them for functions

#define isFive(x) (x == 5) ? 1 : 0

better than using an entire function for it.


    #define isEven(num) (num % 2 ==  0) ? true : false


Smile I like your programming style, you should work with me some time. :P
03-12-2011, 11:45 PM #25
3arc
Meow.
In response to the first post, i came up with this:
    
#import <iostream>
using namespace std;

int main() {
int items = 0;
int i;
float cost;
float currentMoney;
float total;
cout << "How many items? ";
cin >> items;
cout << "How much money do you have? ";
cin >> currentMoney;

for(i = 0; i < items; i++) {
cout << "Enter cost of item: ";
cin >> cost[i];
total += cost[i];
}
if(total <= currentMoney) {
cout << "You have enough money.\n";
cout << "Total Cost: " << total << endl;
}
else if(total > currentMoney) {
cout << "You do not have enough money. You need " << total - currentMoney << " more.\n";
cout << "Total Cost: " << total << endl;
}
}
Last edited by 3arc ; 03-12-2011 at 11:59 PM.
03-12-2011, 11:56 PM #26
kiwimoosical
Bounty hunter
Originally posted by 3arc View Post
In response to the first post, i came up with this:
    
#import <iostream>
using namespace std;

int main() {
int items = 0;
int i;
float cost;
float currentMoney;
float total;
cout << "How many items? ";
cin >> items;
cout << "How much money do you have? ";
cin >> currentMoney;

for(i = 0; i < items; i++) {
cout << "Enter cost of item: ";
cin >> cost;
total += cost;
}
if(total <= currentMoney) {
cout << "You have enough money.\n";
cout << "Total Cost: " << total << endl;
}
else if(total > currentMoney) {
cout << "You do not have enough money. You need " << total - currentMoney << " more.\n";
cout << "Total Cost: " << total << endl;
}
}


Your array is unnecessary, you never use it... Fixed your version.
03-12-2011, 11:58 PM #27
3arc
Meow.
Originally posted by kiwimoosical View Post
Your array is unnecessary, you never use it... Fixed your version.


Oops, i left that in there on accident Not Happy or Sad.
03-13-2011, 03:40 AM #28
kiwimoosical
Bounty hunter
Originally posted by 3arc View Post
Oops, i left that in there on accident Not Happy or Sad.


:P Your welcome (Too short)

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo