My first cpp app

This is a discussion on My first cpp app within the C++ Programming forums, part of the General Programming Boards category; Well at least the basics are really similar to Java. I just need to think of things to do. :dunno: ...

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    My first cpp app

    Well at least the basics are really similar to Java. I just need to think of things to do. :dunno:

    Code:
    // Calc.cpp
    // CompiledMonkey
    
    #include "stdafx.h"
    
    void add()
    {
    	float x, y;
    
    	std::cout << "Enter first number: ";
    	std::cin >> x;
    
    	std::cout << "Enter second number: ";
    	std::cin >> y;
    
    	std::cout << x << " + " << y << " = " << (x + y) << "\n";
    }
    
    void subtract()
    {
    	float x, y;
    
    	std::cout << "Enter first number: ";
    	std::cin >> x;
    
    	std::cout << "Enter second number: ";
    	std::cin >> y;
    
    	std::cout << x << " - " << y << " = " << (x - y) << "\n";
    }
    
    int main()
    {
    	bool flag = true;
    	int option = 0;
    
    	do
    	{
    		// Display menu and get option
    		std::cout << "\n1: Add\n2: Subtract\n3: Exit\n\nPick an operation: ";
    		std::cin >> option;
    
    		switch(option)
    		{
    			case 1:
    				add();
    				break;
    			case 2:
    				subtract();
    				break;
    			case 3:
    				flag = false;
    				break;
    			default:
    				std::cout << "Bad choice!\n\n";
    				break;
    		}
    	}
    	while(flag);
    
    	return 0;
    }

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Good program. But..

    1. Functions are usually defined after main; prototypes are used.

    2. stdafx.h is not standard C++;- use with Visual C++

    Check out C++ tutorials and books.

    What book are you using to help you?
    Mr. C: Author and Instructor

  3. #3
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Originally posted by Mister C
    Good program. But..

    1. Functions are usually defined after main; prototypes are used.

    2. stdafx.h is not standard C++;- use with Visual C++

    Check out C++ tutorials and books.

    What book are you using to help you?
    1. I was thinking it was odd I couldn't have my functions below main. I'm not sure what you mean by prototypes though.

    2. I just started a Win32 console application in VS.NET 2003. It started me out with that.

    I'm not using any books right now. I just started coding. When I ran into a block, I did a quick Google search.

  4. #4
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Prototypes are like:
    Code:
    #includes
    int myfunc(int this, int that);//Prototype
    int main(){
    //Blah blah blah
    }
    myfunc(int this, int that){
    //Blah blah blah
    }
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  5. #5
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Oh I see. So I should make prototypes of my functions at the top?

  6. #6
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    >So I should make prototypes of my functions at the top?
    After the includes and global variables(yes, I no they're bad habits).
    I always do that. Looks neater. Or you could put your functions in a .h file and include that.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  7. #7
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Yeah, I was about to ask about header files. Would a header just have the prototype or the actual implementation as well?

  8. #8
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    >>Would a header just have the prototype or the actual implementation as well?

    You can do either. If your functions reference each other you should do prototypes.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  9. #9
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Cool. Anymore advice?

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Function prototypes should be placed in a header-file and the function implementation should be placed in a source-file. If your functions are used in more files, then you only need to include the header-file with the prototypes in those files.

  11. #11
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Sounds like solid design to me. Thanks.

    Anyone want to comment on my code style? Am I doing anything wrong? More ideas for small time projects like this are welcome as well.

  12. #12
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    This:
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    instead of:

    [CODE]
    #include<iostream.h>
    #include<string.h>
    using namespace std;
    [CODE]

    Leave out the .h on files, add
    Code:
    using namespace std;
    and for older files, like stdlib.h do cstdlib.
    Last edited by fuh; 06-15-2003 at 01:46 PM.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  13. #13
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    It doesn't matter if you put your functions before or after main... but prototypes are a good idea.

    A way to simplify your program:
    Group the inputting of the first two numbers into a seperate function (or int main, if you prefer), instead of one copy in addition and one copy in subtraction. ie:
    Code:
    // Calc.cpp
    // CompiledMonkey
    
    #include "stdafx.h"
    void add();
    void subtract();
    void input_two_numbers(float&, float&);
    
    void input_two_numbers(float& x, float& y)
    {
    	std::cout << "Enter first number: ";
    	std::cin >> x;
    
    	std::cout << "Enter second number: ";
    	std::cin >> y;
    }
    void add()
    {
    	float x, y;
    	input_two_numbers(x,y);
    
    	std::cout << x << " + " << y << " = " << (x + y) << "\n";
    }
    
    void subtract()
    {
    	float x, y;
    	input_two_numbers(x,y);
    
    	std::cout << x << " - " << y << " = " << (x - y) << "\n";
    }
    
    int main()
    {
    	bool flag = true;
    	int option = 0;
    
    	do
    	{
    		// Display menu and get option
    		std::cout << "\n1: Add\n2: Subtract\n3: Exit\n\nPick an operation: ";
    		std::cin >> option;
    
    		switch(option)
    		{
    			case 1:
    				add();
    				break;
    			case 2:
    				subtract();
    				break;
    			case 3:
    				flag = false;
    				break;
    			default:
    				std::cout << "Bad choice!\n\n";
    				break;
    		}
    	}
    	while(flag);
    
    	return 0;
    }

  14. #14
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    > More ideas for small time projects like this are welcome as well.

    You could do something simple like implementing a datastructure to learn about how to use the language and its language elements and constructions.

    Also you should experiment a bit with classes to see what C++ and Java have common and what is different. For example see how abstract classes are implemented in C++ and how they differ from Java. And experiment a bit with inheritance.

    When I started programming in Java, with a background in C++, I found a lot of things were similar. However, some things were a bit different and it is good to find out what the impact of the differences are. C++ for example allows multiple inheritance by classes, for Java you can something like that using interfaces.

  15. #15
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    CompiledMonkey,
    have you learned about classes yet? If so, or when you do, you can make the calculator object oriented.

    P.S.:I attached my first calculator here. Before you look at the source try adding a square root function. I didn't get to this, but you could also add powers. If you're stumped, you can use sqrt() or pwr() in math.h (or is the newer one cmath or math?)
    Attached Files Attached Files
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. Replies: 2
    Last Post: 04-09-2006, 07:20 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. best program to start
    By gooddevil in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-28-2004, 05:56 PM
  5. pasword app
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 10:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21