Adblock breaks this site

[C++] My First Script [BASIC]

Discussion in 'Programming General' started by htaed, Jul 8, 2007.

  1. htaed

    htaed Forum Addict
    Banned

    Joined:
    Dec 19, 2005
    Posts:
    336
    Referrals:
    0
    Sythe Gold:
    0
    [C++] My First Script [BASIC]

    Okay, so i'm trying to learn C++
    After researching a bit, i threw together this code.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string fname, lname;
    	int n1, n2, n3, n4, n5, n6, n7;
    
    	cout << "Hello there, please enter your first name:\n";
    	cin >> fname;
    	cout << "Okay, how about your last name:\n";
    	cin >> lname;
    	cout << "Okay, so your " << fname << " " << lname << endl;
    	cout << "\nLets do a sum!\n";
    	cout << "Please enter a number\n";
    	cin >> n1;
    	cout << "Okay, " << n1 << " Good, please enter another number:\n";
    	cin >> n2;
    	n3 = n1 + n2;
    	cout << "Righty, So.. " << n1 << " + " << n2 << " = " << n3 << endl;
    	cout << "\nLets do another quck sum\n";
    	cout << "Please enter a number:\n";
    	cin >> n4;
    	cout << "Please enter another number:\n";
    	cin >> n5;
    	n6 = n4 * n5;
    	cout << "\nOkie Doke, " << n4 << " * " << n5 << " = " << n6 << endl;
    	cout << "Okay, one last sum!\n";
    	cout << "So as we said " << n1 << " + " << n2 << " = " << n4 << " and " << n4 << " * " << n5 << " = " << n6 << " right.. so what if we add the totals\n together\n";
    	n7 = n3 + n6;
    	cout << "They total to " << n7 << ". Thanks for using my program!\n";
    	return 0;
    }
    	
    	
    Now i know i could of not used the n3 and n6 but i wanted to use them so i could total at the end, if that makes any sence to you.

    So, is there anyway i could improve that code? make it smaller? simpler?

    Thank you.
     
  2. hotfoot982

    hotfoot982 Guest

    Referrals:
    0
    [C++] My First Script [BASIC]

    I don't think you could make it any smaller or simpler.
    If your worrying about speed (why would you lol) than it will still run the same speed.

    Also, I noticed for this program you would have to open it in cmd.

    How about trying to put this right before "return0;" :
    cin.get();
    cin.ignore();

    It makes it A LOT better.



    - hotfoot982
     
  3. ekWeb

    ekWeb Guest

    Referrals:
    0
    [C++] My First Script [BASIC]

    Well yeah the code looks good but the last message where it says, "Thanks for using my program!" will not be shown so if you want to end it and nothing needed you can just write "cin>>n5;" there as an example so the program will stop and expect an input so u can see the last message. I don't know if you understand but instead of n5 we couldve used any of the n's or even fname or lname just to stop and see the last message.

    -ekWeb
     
  4. Zeh Progage

    Zeh Progage Guest

    Referrals:
    0
    [C++] My First Script [BASIC]

    You could also use system("PAUSE");
    :D (But, for portability, use the ones specified above)
     
  5. the emogod

    the emogod Forum Addict

    Joined:
    Feb 17, 2007
    Posts:
    318
    Referrals:
    0
    Sythe Gold:
    0
    [C++] My First Script [BASIC]

    Its wrong.. it says n1 + n2 = n4... which is VERY wrong... it only works like 1 percent of the time... it should be

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string fname, lname;
    	int n1, n2, n3, n4, n5, n6, n7;
    
    	cout << "Hello there, please enter your first name:\n";
    	cin >> fname;
    	cout << "Okay, how about your last name:\n";
    	cin >> lname;
    	cout << "Okay, so your " << fname << " " << lname << endl;
    	cout << "\nLets do a sum!\n";
    	cout << "Please enter a number\n";
    	cin >> n1;
    	cout << "Okay, " << n1 << " Good, please enter another number:\n";
    	cin >> n2;
    	n3 = n1 + n2;
    	cout << "Righty, So.. " << n1 << " + " << n2 << " = " << n3 << endl;
    	cout << "\nLets do another quck sum\n";
    	cout << "Please enter a number:\n";
    	cin >> n4;
    	cout << "Please enter another number:\n";
    	cin >> n5;
    	n6 = n4 * n5;
    	cout << "\nOkie Doke, " << n4 << " * " << n5 << " = " << n6 << endl;
    	cout << "Okay, one last sum!\n";
    	cout << "So as we said " << n1 << " + " << n2 << " = " << n3 << " and " << n4 << " * " << n5 << " = " << n6 << " right.. so what if we add the totals\n together\n";
    	n7 = n3 + n6;
    	cout << "They total to " << n7 << ". Thanks for using my program!\n";
        system("PAUSE");
    	return 0;
    }
     
< Simple C++ Program -- Colored Text | Need help >


 
 
Adblock breaks this site