Help and Random Questions

This is a discussion on Help and Random Questions within the C++ Programming forums, part of the General Programming Boards category; I have a few questions about random things about programming and any help would be appreciated. First off, I'm a ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Cool Help and Random Questions

    I have a few questions about random things about programming and any help would be appreciated.

    First off, I'm a beginner as you can probably tell by my code and I would like to know how I could make it so my calculator can have any number of inputs to be added, subtracted etc instead of just having 2 and not having to put in a bunch of extra variables that might not even be needed. Also any tips on improving the look of my code would help me.

    I'm using visual C++ 2008 to practice coding and I'm wondering how would I be able to transfer my calculator program to another computer. Also I have Windows Vista 64-bit if that matters.

    Pretty soon I'm going to be taking a class on C++ as part of my technical degree; after finishing the degree I'll be transferring to a four year college. I'm wondering what to expect from the class because the course description online just says, "you will learn C++".

    One thing that I find interesting is AI and if you can do any of that in C++ and if so where could i learn about it?


    Code for my calculator program, which works fine by the way:

    Code:
    #include <iostream>
    using namespace std;
    
    int mult(int a, int b);
    int divide(int a, int b);
    int add(int a, int b);
    int subt(int a, int b);
    
    
    int main()
    {
    	int a, b, x;
    	char cChar, yesno;
    
    cout<< "Welcome to my amazing calculator program.\n";
    
    redo:													//goto redo label
    	cout<<"1.Multiply \n";
    	cout<<"2.Divide \n";
    	cout<<"3.Add \n";
    	cout<<"4.Subtract \n";
    	cout<<"Enter the number of your selection: \n\n";
    	
    	cin>> x;
    
    	switch (x){
    		case 1:
    			cout<<"Type two whole numbers you want MULTIPLIED \n";                // MULTIPLY
    			cin>>a >> b;
    			cout<<"Your answer is: ";
    				cout<< mult(a,b)<<endl;
    									cin.get(cChar);
    				cout<<"\n";
    				cout<<"Again? (y/n)"<<endl;
    				cin>>yesno;
    					if(yesno=='y'){
    					cout<<"\n";
    				goto redo;
    				}
    			else break;
    				
    		case 2:
    redo_div:															//goto redo_div label
    			cout<<"Type two whole numbers you want DIVIDED \n";					// DIVIDE
    			cin>>a >> b;
    			if(b==0){
    				cout<<"DONT DO THAT :)"<<endl;					//Makes sure cannot divide by zero
    				goto redo_div;
    				}
    			cout<<"Your answer is: ";
    				cout<< divide(a,b)<<endl;
    									cin.get(cChar);
    				cout<<"\n";
    				cout<<"Again? (y/n)"<<endl;
    				cin>>yesno;
    					if(yesno=='y'){
    					cout<<"\n";
    				goto redo;
    			}
    			else break;
    				
    		case 3:
    			cout<<"Type two whole numbers you want ADDED \n";						// ADD
    			cin>>a >> b;	
    			cout<<"Your answer is: ";
    				cout<< add(a,b)<<endl;
    									cin.get(cChar);
    				cout<<"\n";
    				cout<<"Again? (y/n)"<<endl;
    				cin>>yesno;
    					if(yesno=='y'){
    					cout<<"\n";
    				goto redo;
    			}
    			else break;
    				
    		case 4:
    			cout<<"Type two whole numbers you want SUBTRACTED \n";				// SUBTRACT
    			cin>>a >> b;
    			cout<<"Your answer is: ";
    				cout<< subt(a,b)<<endl;
    									cin.get(cChar);
    				cout<<"\n";
    				cout<<"Again? (y/n)"<<endl;
    				cin>>yesno;
    					if(yesno=='y'){
    					cout<<"\n";
    				goto redo;
    			}
    			else break;
    				
    		default:															// ERROR
    			cout<<"You did something wrong...";	
    									cin.get(cChar);	
    			cout<<"\n";
    			cout<<"Again? (y/n)"<<endl;
    			cin>>yesno;
    				if(yesno=='y'){
    				cout<<"\n";
    			goto redo;
    			}
    			else break;
    	}
    	
    	return 0;
    		
    }
    
    int mult(int a, int b)
    	{
    	return a*b;
    	}
    int divide(int a, int b)
    	{
    	return a/b;
    	}
    int add(int a, int b)
    	{
    		return a+b;
    	}
    int subt(int a, int b)
    	{
    		return a-b;	
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1. Don't use goto when not really needed. You should use a while loop. Like:
    Code:
    case 1:
        while(yesno == 'y') {
             ...
        }
    2. You can also you a while loop when you have multiple numbers. Lets say you multiply:
    Code:
    int result;
    cin >> a;
    while(1) {
    cin >> b;
        result *= b;
    }
    The above will just not end of course. I won't give right away the answer. You can try it and if you are not able to do it, just post again.

    3. Your program is on the PC. It is just an .exe file. Search your hard drive for your project name or the name of your program. I would guess is somewhere like this \MyDocuments\VisualStudio2008\Projects\YourProject \Debug\bin\YourProgram.exe

    4. Well, the code is probably not copy/pasted properly here so can't really say

    5. It would have been nicer if you avoided some code that is reused. Like:
    Code:
    int result;
    cout << "Type two numbers you want ";
    switch (x) {
       case 1:
           cout << "ADDED: ";
           break;
       ...
    }
    cin >> a >> b;
    cin.get();
    switch (x) {
       case 1:
           result = add(a, b);
           break;
       ...
    }
    cout << "Your result is: " << result << endl;
    This is not just for looks. It is just good practice. Cause if you want to change something, like the way you get input, you will change it once. Not four time. This will generally avoid you forgetting to change something and creating all sort of bugs

    6. What if somebody divides with zero? Generally it is a good idea to check for this in a calculator

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    1. I'm not sure how I would use a while instead of a goto because how would I get it to repeat the whole program again if they answered 'y'?

    2. I'm not really sure what you mean.

    3. I have found the .exe file before and transferred it to another computer a 32-bit XP but it wouldn't load up.

    5. I just copy and pasted most of the code so it was easy, but if I did it the way you are suggesting would it make the program more efficient?

    6. If you go to the division part of my program you can see the check divide by zero thing.

    Code:
    cout<<"Type two whole numbers you want DIVIDED \n";					// DIVIDE
    			cin>>a >> b;
    			if(b==0){
    				cout<<"DONT DO THAT :)"<<endl;					//Makes sure cannot divide by zero
    				goto redo_div;
    				}

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Garfield910 View Post
    1. I'm not sure how I would use a while instead of a goto because how would I get it to repeat the whole program again if they answered 'y'?
    Code:
    their_answer = 'y';
    while(their_answer == 'y') { the_whole_program; }
    Quote Originally Posted by Garfield910 View Post
    3. I have found the .exe file before and transferred it to another computer a 32-bit XP but it wouldn't load up.
    "it wouldn't load up." Generally there is some sort of error or symptom, I mean, it didn't just pop up a message box with "No." and an OK button on it. (And if it did, I'd still want to know that.)

    You mentioned 64-bit. If you're going to copy the .exe to a 32-bit machine, you need to make sure that the compiler compiles for x86, not amd64 (or x86_64/whatever name MS has invented for 64-bit. Note there might be IA64, which is a different thing entirely.) It's an option. Basically, an amd64 compatible machine can run 32-bit x86 executables, given 32-bit versions of DLLs, but a 32-bit x86 has no hope of running 64-bit executables. (Short of emulation.)

    Quote Originally Posted by Garfield910 View Post
    5. I just copy and pasted most of the code so it was easy, but if I did it the way you are suggesting would it make the program more efficient?
    It'll shorten your code size, which is a good thing. Less code is easier to maintain, and will prolong you pulling your hair out because you have more code than you know what to do with.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

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