Hey guys,

This is my first official post on DaniWeb :)

I am new at programming in C++ using visual studios, and right now I am fooling around with storing user input into Arrays.

I am having a problem with my code, using an array size of 3 to store the users input into the first 2 spots [0 and 1] and then making a calculation in the 3rd spot[2].

Here is my code:

#include <iostream>

using namespace std;

int main()	{

	double userInput[3];		//array can only hold *3* elements

	cout<<"Enter the number of cars you wish to buy: ";
	cin>> userInput[0];			// users input stored into array 0

	cout<<"Enter the amount of money you wish to spend for one car: ";
	cin>> userInput[1];

	//the last array will times the number of cars you wish to buy times
	//the amount you wish to spend for one car.
	//the value will be stored in the last array

	userInput[2] = userInput[0] * userInput[1];

	cout<< "According to my calculations... If you were to buy "<<userInput[0]<<" cars, for $"<< userInput[1]<<" each, then the total money you will spend will be $" <<userInput[2]<<endl;

	return 0;


P.S. How can I spread out the last 'cout' of my code onto multiple lines without disturbing the build so that it works!?

Thanks guys and I hope to hear a respond very soon!

- Ben.

Edited 5 Years Ago by Nick Evan: Added [CODE] tags

When I try to build it, I am getting an error '_main already defined in basic array.obj'

I don't see whats wrong with the code..

To make your code neater you can hit enter(just not in the middle of your strings), like:

cout<<"According to my calculations...if you were to buy..."
    <<userinput[0]<<" cars for $"

as the compiler will get rid of the whitespace.

The nice thing about cout is that you can simply keep giving it input without an "endl" or a "\n" and it will keep it on the same line.

cout<<" yada yada"

and it will be all on one line too. Experiment with it a little.

As far as your second problem, you probably have a second cpp file in your project called whatevermyprojnameis.cpp, which contains a main() by default. Go ahead and either omit that file from your project, or delete the code that's put in there by default.

Edited 5 Years Ago by jonsca: n/a

Solved all problems in 1 post. Nice :)

thank you for the extremely helpful advice on cout, and also for fixing my problem!

I'm new to visual, and so I was trying to organize my tutorial coding into my 'Arrays' source files, but I guess you have to separate main programs?

There can only be one main per project, as the compiler has to know where the entry point is. It can be in the same source file as your other code, or by itself.

This question has already been answered. Start a new discussion instead.