0

Hi guys.

What my problem is basically either some typo, or I am doing something extremelly wrong. Can't think of something else.

After some time off with C++ I got back with it, creating the simple project to see how well my memory serves me. I am pretty sure that I'm not doing something wrong with this code.

#include <iostream>

using namespace std;

int money= 1000;
string user;

int main()
{
	cout << "Enter username." << endl;
	cin >> user;
	cout << "Welcome! " << endl;
	cout << "You have : " << money << " dollars left.\n" ;

}

Thanks for the help.

3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by UltimateKnight
0

I am pretty sure that I'm not doing something wrong with this code.

You forgot to include <string>, and the code also does nothing with user after the input call. The following offers some corrections, though your use of global variables is still questionable:

#include <iostream>
#include <string>
 
using namespace std;
 
int money = 1000;
string user;
 
int main()
{
    cout << "Enter username." << endl;
    cin >> user;
    cout << "Welcome, " << user << "!" << endl;
    cout << "You have : " << money << " dollars left.\n" ;
}
0

That is quite foolish of my side, forgetting such thing. My apologies. Also, I didn't need the input call for anything yet, just wanted to know why it doesn't work.

Anyway, about the global variables, is that a wrong method?

Edited by UltimateKnight: n/a

0

Anyway, about the global variables, is that a wrong method?

Variables should have the smallest scope possible. Global variables are especially troublesome because they're visible and usable across the entire program, even areas that don't need access.

0

Can that cause problems?

<sarcasm>
No, we just arbitrarily picked something to bash because it makes programming seem more like a voodoo art. :icon_rolleyes:
</sarcasm>

I should declare local variables instead?

You should declare variables with the smallest scope possible.

0

<sarcasm>
No, we just arbitrarily picked something to bash because it makes programming seem more like a voodoo art. :icon_rolleyes:
</sarcasm>


You should declare variables with the smallest scope possible.

Thanks! :D

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.