• Log in or sign up for an account here: https://www.sythe.org/login

[C++] My First Script [BASIC]

htaed

Forum Addict
Joined
Dec 19, 2005
Sythe Gold
0
Messages
336
Age
38
Location
Anchorage, Alaska
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.
 
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
 
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
 
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
You could also use system("PAUSE");
:D (But, for portability, use the ones specified above)
 
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;
}
 
Runescape Gamblingrunescape gambling
Back
Top