[C++] My First Script [BASIC]

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

[C++] My First Script [BASIC]
  1. Unread #1 - Jul 8, 2007 at 9:53 AM
  2. htaed
    Joined:
    Dec 19, 2005
    Posts:
    336
    Referrals:
    0
    Sythe Gold:
    0

    htaed Forum Addict
    Banned

    [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.
     
  3. Unread #2 - Jul 9, 2007 at 4:33 AM
  4. hotfoot982
    Referrals:
    0

    hotfoot982 Guest

    [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
     
  5. Unread #3 - Aug 1, 2007 at 11:40 PM
  6. ekWeb
    Referrals:
    0

    ekWeb Guest

    [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
     
  7. Unread #4 - Aug 13, 2007 at 3:46 AM
  8. Zeh Progage
    Referrals:
    0

    Zeh Progage Guest

    [C++] My First Script [BASIC]

    You could also use system("PAUSE");
    :D (But, for portability, use the ones specified above)
     
  9. Unread #5 - Aug 14, 2007 at 2:38 AM
  10. the emogod
    Joined:
    Feb 17, 2007
    Posts:
    318
    Referrals:
    0
    Sythe Gold:
    0

    the emogod Forum Addict

    [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 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site