Storage options for Loops? C#
Hello i am learning programming and trying to create a simple console app for users to buy movie tickets.
i am currently using a lot of loops and i would like to know if there is a way to:
1:Store user input from the loops.
2:take stored data from the loops and add it into a final tally.
I have tried Using an Array to store user inputs from the loop but when i want to take the data to be then stored in a final tally i get a error: Operator cannot be applied to operands of type 'int' and 'int'.
I do not want to post my full source but hopfully seeing my vairables should help you understand what i need to track.
Thank you in advanced, any help is welcome.
//Ticket sale math Values
double TotalIncome;
double IncomeFromChild;
double IncomeFromAdult;
double IncomeFromStudent;
double VATFinal;
//Tickets Sold (name of film or total)
double FilmA = 33;
double FilmB = 15;
double TotalTicketsSold;
float PeopleServed;
//Ticket Sale Counters
double ChildTickets = 0;
double Adulttickets = 0;
double StudentTickets = 0;
//Array For Ticket prices, sales and user input
var TicketChoices = new int[3];
var TicketAmount = new int[48];
//Maxxium tickets allowed for each film
int FilmA = 33;
int FilmB = 15;
int TotalTickets = 48;
c# loops
add a comment |
Hello i am learning programming and trying to create a simple console app for users to buy movie tickets.
i am currently using a lot of loops and i would like to know if there is a way to:
1:Store user input from the loops.
2:take stored data from the loops and add it into a final tally.
I have tried Using an Array to store user inputs from the loop but when i want to take the data to be then stored in a final tally i get a error: Operator cannot be applied to operands of type 'int' and 'int'.
I do not want to post my full source but hopfully seeing my vairables should help you understand what i need to track.
Thank you in advanced, any help is welcome.
//Ticket sale math Values
double TotalIncome;
double IncomeFromChild;
double IncomeFromAdult;
double IncomeFromStudent;
double VATFinal;
//Tickets Sold (name of film or total)
double FilmA = 33;
double FilmB = 15;
double TotalTicketsSold;
float PeopleServed;
//Ticket Sale Counters
double ChildTickets = 0;
double Adulttickets = 0;
double StudentTickets = 0;
//Array For Ticket prices, sales and user input
var TicketChoices = new int[3];
var TicketAmount = new int[48];
//Maxxium tickets allowed for each film
int FilmA = 33;
int FilmB = 15;
int TotalTickets = 48;
c# loops
1
This message ' Operator cannot be applied to operands of type 'int' and 'int'' actually means you're trying to do some sort of arithmetic operation, say an addition, between an array of int and an int which makes no sense. When you're manipulating the array, if let's say you want to add 5 to every value it contains, you need to loop within the array and add 5 to every value contained in it.
– Kevin Avignon
Nov 21 '18 at 22:26
1
Also, the variablesFilmA
andFilmB
are each declared twice. How does this compile? Anyway, I suggest that you a) Tell us exactly what the program needs to do; and b) Give us all of your code. This doesn't seem like a large program, we can take it.
– Vilx-
Nov 21 '18 at 22:29
See also Best Data Type to Use for Currency --things that should bedecimal
are set todouble
(there's rounding difference with the two) , and things that would do better asint
are set todouble
. Why usefloat
instead ofint
for a single whole-number?
– Symon
Nov 21 '18 at 22:33
1
You need to post a minimal complete verifiable example; the code you posted doesn't show the error you are talking about, has other errors you don't mention, and includes too much irrelevant code.
– Dour High Arch
Nov 21 '18 at 22:50
add a comment |
Hello i am learning programming and trying to create a simple console app for users to buy movie tickets.
i am currently using a lot of loops and i would like to know if there is a way to:
1:Store user input from the loops.
2:take stored data from the loops and add it into a final tally.
I have tried Using an Array to store user inputs from the loop but when i want to take the data to be then stored in a final tally i get a error: Operator cannot be applied to operands of type 'int' and 'int'.
I do not want to post my full source but hopfully seeing my vairables should help you understand what i need to track.
Thank you in advanced, any help is welcome.
//Ticket sale math Values
double TotalIncome;
double IncomeFromChild;
double IncomeFromAdult;
double IncomeFromStudent;
double VATFinal;
//Tickets Sold (name of film or total)
double FilmA = 33;
double FilmB = 15;
double TotalTicketsSold;
float PeopleServed;
//Ticket Sale Counters
double ChildTickets = 0;
double Adulttickets = 0;
double StudentTickets = 0;
//Array For Ticket prices, sales and user input
var TicketChoices = new int[3];
var TicketAmount = new int[48];
//Maxxium tickets allowed for each film
int FilmA = 33;
int FilmB = 15;
int TotalTickets = 48;
c# loops
Hello i am learning programming and trying to create a simple console app for users to buy movie tickets.
i am currently using a lot of loops and i would like to know if there is a way to:
1:Store user input from the loops.
2:take stored data from the loops and add it into a final tally.
I have tried Using an Array to store user inputs from the loop but when i want to take the data to be then stored in a final tally i get a error: Operator cannot be applied to operands of type 'int' and 'int'.
I do not want to post my full source but hopfully seeing my vairables should help you understand what i need to track.
Thank you in advanced, any help is welcome.
//Ticket sale math Values
double TotalIncome;
double IncomeFromChild;
double IncomeFromAdult;
double IncomeFromStudent;
double VATFinal;
//Tickets Sold (name of film or total)
double FilmA = 33;
double FilmB = 15;
double TotalTicketsSold;
float PeopleServed;
//Ticket Sale Counters
double ChildTickets = 0;
double Adulttickets = 0;
double StudentTickets = 0;
//Array For Ticket prices, sales and user input
var TicketChoices = new int[3];
var TicketAmount = new int[48];
//Maxxium tickets allowed for each film
int FilmA = 33;
int FilmB = 15;
int TotalTickets = 48;
c# loops
c# loops
asked Nov 21 '18 at 22:22
dood1dood1
72
72
1
This message ' Operator cannot be applied to operands of type 'int' and 'int'' actually means you're trying to do some sort of arithmetic operation, say an addition, between an array of int and an int which makes no sense. When you're manipulating the array, if let's say you want to add 5 to every value it contains, you need to loop within the array and add 5 to every value contained in it.
– Kevin Avignon
Nov 21 '18 at 22:26
1
Also, the variablesFilmA
andFilmB
are each declared twice. How does this compile? Anyway, I suggest that you a) Tell us exactly what the program needs to do; and b) Give us all of your code. This doesn't seem like a large program, we can take it.
– Vilx-
Nov 21 '18 at 22:29
See also Best Data Type to Use for Currency --things that should bedecimal
are set todouble
(there's rounding difference with the two) , and things that would do better asint
are set todouble
. Why usefloat
instead ofint
for a single whole-number?
– Symon
Nov 21 '18 at 22:33
1
You need to post a minimal complete verifiable example; the code you posted doesn't show the error you are talking about, has other errors you don't mention, and includes too much irrelevant code.
– Dour High Arch
Nov 21 '18 at 22:50
add a comment |
1
This message ' Operator cannot be applied to operands of type 'int' and 'int'' actually means you're trying to do some sort of arithmetic operation, say an addition, between an array of int and an int which makes no sense. When you're manipulating the array, if let's say you want to add 5 to every value it contains, you need to loop within the array and add 5 to every value contained in it.
– Kevin Avignon
Nov 21 '18 at 22:26
1
Also, the variablesFilmA
andFilmB
are each declared twice. How does this compile? Anyway, I suggest that you a) Tell us exactly what the program needs to do; and b) Give us all of your code. This doesn't seem like a large program, we can take it.
– Vilx-
Nov 21 '18 at 22:29
See also Best Data Type to Use for Currency --things that should bedecimal
are set todouble
(there's rounding difference with the two) , and things that would do better asint
are set todouble
. Why usefloat
instead ofint
for a single whole-number?
– Symon
Nov 21 '18 at 22:33
1
You need to post a minimal complete verifiable example; the code you posted doesn't show the error you are talking about, has other errors you don't mention, and includes too much irrelevant code.
– Dour High Arch
Nov 21 '18 at 22:50
1
1
This message ' Operator cannot be applied to operands of type 'int' and 'int'' actually means you're trying to do some sort of arithmetic operation, say an addition, between an array of int and an int which makes no sense. When you're manipulating the array, if let's say you want to add 5 to every value it contains, you need to loop within the array and add 5 to every value contained in it.
– Kevin Avignon
Nov 21 '18 at 22:26
This message ' Operator cannot be applied to operands of type 'int' and 'int'' actually means you're trying to do some sort of arithmetic operation, say an addition, between an array of int and an int which makes no sense. When you're manipulating the array, if let's say you want to add 5 to every value it contains, you need to loop within the array and add 5 to every value contained in it.
– Kevin Avignon
Nov 21 '18 at 22:26
1
1
Also, the variables
FilmA
and FilmB
are each declared twice. How does this compile? Anyway, I suggest that you a) Tell us exactly what the program needs to do; and b) Give us all of your code. This doesn't seem like a large program, we can take it.– Vilx-
Nov 21 '18 at 22:29
Also, the variables
FilmA
and FilmB
are each declared twice. How does this compile? Anyway, I suggest that you a) Tell us exactly what the program needs to do; and b) Give us all of your code. This doesn't seem like a large program, we can take it.– Vilx-
Nov 21 '18 at 22:29
See also Best Data Type to Use for Currency --things that should be
decimal
are set to double
(there's rounding difference with the two) , and things that would do better as int
are set to double
. Why use float
instead of int
for a single whole-number?– Symon
Nov 21 '18 at 22:33
See also Best Data Type to Use for Currency --things that should be
decimal
are set to double
(there's rounding difference with the two) , and things that would do better as int
are set to double
. Why use float
instead of int
for a single whole-number?– Symon
Nov 21 '18 at 22:33
1
1
You need to post a minimal complete verifiable example; the code you posted doesn't show the error you are talking about, has other errors you don't mention, and includes too much irrelevant code.
– Dour High Arch
Nov 21 '18 at 22:50
You need to post a minimal complete verifiable example; the code you posted doesn't show the error you are talking about, has other errors you don't mention, and includes too much irrelevant code.
– Dour High Arch
Nov 21 '18 at 22:50
add a comment |
1 Answer
1
active
oldest
votes
If you want to collect a bunch of something, a good first step is to create a class or struct.
Here's a basic idea - I'm not going to get into best practices or anything:
public class UserInput
{
public decimal IncomeFromChild { get; set; }
public decimal IncomeFromAdult { get; set; }
public decimal IncomeFromStudent { get; set; }
public decimal TotalIncome => IncomeFromChild + IncomeFromAdult + IncomeFromStudent;
}
As you receive each set of values, you can create an instance of UserInput
.
var myInput = new UserInput {IncomeFromChild = 5.00M, IncomeFromAdult = 3.5M};
That makes the original question much easier to answer. How do we store these? When we want to store some of something, we use a collection.
Perhaps the most commonly used collection is a List
, because you can just add stuff to it.
Add the import using System.Collections.Generic;
to the top of your file first.
var myListOfInputs = new List<UserInput>();
myListOfInputs.Add(myInput);
Then you can look at all the items in the list like this:
decimal total = 0M;
foreach(var input in myListOfInputs)
{
total = total + input.TotalIncome;
}
There are also a lot of extension methods for performing operations on collections. If you add this import to the beginning of a file:
using System.Linq;
You can use this:
var total = myListOfInputs.Sum(i => i.TotalIncome);
It's essentially the same as the loop above, just shorter to write. It doesn't hurt to learn by writing some things like that out the long way.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421275%2fstorage-options-for-loops-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to collect a bunch of something, a good first step is to create a class or struct.
Here's a basic idea - I'm not going to get into best practices or anything:
public class UserInput
{
public decimal IncomeFromChild { get; set; }
public decimal IncomeFromAdult { get; set; }
public decimal IncomeFromStudent { get; set; }
public decimal TotalIncome => IncomeFromChild + IncomeFromAdult + IncomeFromStudent;
}
As you receive each set of values, you can create an instance of UserInput
.
var myInput = new UserInput {IncomeFromChild = 5.00M, IncomeFromAdult = 3.5M};
That makes the original question much easier to answer. How do we store these? When we want to store some of something, we use a collection.
Perhaps the most commonly used collection is a List
, because you can just add stuff to it.
Add the import using System.Collections.Generic;
to the top of your file first.
var myListOfInputs = new List<UserInput>();
myListOfInputs.Add(myInput);
Then you can look at all the items in the list like this:
decimal total = 0M;
foreach(var input in myListOfInputs)
{
total = total + input.TotalIncome;
}
There are also a lot of extension methods for performing operations on collections. If you add this import to the beginning of a file:
using System.Linq;
You can use this:
var total = myListOfInputs.Sum(i => i.TotalIncome);
It's essentially the same as the loop above, just shorter to write. It doesn't hurt to learn by writing some things like that out the long way.
add a comment |
If you want to collect a bunch of something, a good first step is to create a class or struct.
Here's a basic idea - I'm not going to get into best practices or anything:
public class UserInput
{
public decimal IncomeFromChild { get; set; }
public decimal IncomeFromAdult { get; set; }
public decimal IncomeFromStudent { get; set; }
public decimal TotalIncome => IncomeFromChild + IncomeFromAdult + IncomeFromStudent;
}
As you receive each set of values, you can create an instance of UserInput
.
var myInput = new UserInput {IncomeFromChild = 5.00M, IncomeFromAdult = 3.5M};
That makes the original question much easier to answer. How do we store these? When we want to store some of something, we use a collection.
Perhaps the most commonly used collection is a List
, because you can just add stuff to it.
Add the import using System.Collections.Generic;
to the top of your file first.
var myListOfInputs = new List<UserInput>();
myListOfInputs.Add(myInput);
Then you can look at all the items in the list like this:
decimal total = 0M;
foreach(var input in myListOfInputs)
{
total = total + input.TotalIncome;
}
There are also a lot of extension methods for performing operations on collections. If you add this import to the beginning of a file:
using System.Linq;
You can use this:
var total = myListOfInputs.Sum(i => i.TotalIncome);
It's essentially the same as the loop above, just shorter to write. It doesn't hurt to learn by writing some things like that out the long way.
add a comment |
If you want to collect a bunch of something, a good first step is to create a class or struct.
Here's a basic idea - I'm not going to get into best practices or anything:
public class UserInput
{
public decimal IncomeFromChild { get; set; }
public decimal IncomeFromAdult { get; set; }
public decimal IncomeFromStudent { get; set; }
public decimal TotalIncome => IncomeFromChild + IncomeFromAdult + IncomeFromStudent;
}
As you receive each set of values, you can create an instance of UserInput
.
var myInput = new UserInput {IncomeFromChild = 5.00M, IncomeFromAdult = 3.5M};
That makes the original question much easier to answer. How do we store these? When we want to store some of something, we use a collection.
Perhaps the most commonly used collection is a List
, because you can just add stuff to it.
Add the import using System.Collections.Generic;
to the top of your file first.
var myListOfInputs = new List<UserInput>();
myListOfInputs.Add(myInput);
Then you can look at all the items in the list like this:
decimal total = 0M;
foreach(var input in myListOfInputs)
{
total = total + input.TotalIncome;
}
There are also a lot of extension methods for performing operations on collections. If you add this import to the beginning of a file:
using System.Linq;
You can use this:
var total = myListOfInputs.Sum(i => i.TotalIncome);
It's essentially the same as the loop above, just shorter to write. It doesn't hurt to learn by writing some things like that out the long way.
If you want to collect a bunch of something, a good first step is to create a class or struct.
Here's a basic idea - I'm not going to get into best practices or anything:
public class UserInput
{
public decimal IncomeFromChild { get; set; }
public decimal IncomeFromAdult { get; set; }
public decimal IncomeFromStudent { get; set; }
public decimal TotalIncome => IncomeFromChild + IncomeFromAdult + IncomeFromStudent;
}
As you receive each set of values, you can create an instance of UserInput
.
var myInput = new UserInput {IncomeFromChild = 5.00M, IncomeFromAdult = 3.5M};
That makes the original question much easier to answer. How do we store these? When we want to store some of something, we use a collection.
Perhaps the most commonly used collection is a List
, because you can just add stuff to it.
Add the import using System.Collections.Generic;
to the top of your file first.
var myListOfInputs = new List<UserInput>();
myListOfInputs.Add(myInput);
Then you can look at all the items in the list like this:
decimal total = 0M;
foreach(var input in myListOfInputs)
{
total = total + input.TotalIncome;
}
There are also a lot of extension methods for performing operations on collections. If you add this import to the beginning of a file:
using System.Linq;
You can use this:
var total = myListOfInputs.Sum(i => i.TotalIncome);
It's essentially the same as the loop above, just shorter to write. It doesn't hurt to learn by writing some things like that out the long way.
answered Nov 21 '18 at 23:02
Scott HannenScott Hannen
12.7k1425
12.7k1425
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421275%2fstorage-options-for-loops-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This message ' Operator cannot be applied to operands of type 'int' and 'int'' actually means you're trying to do some sort of arithmetic operation, say an addition, between an array of int and an int which makes no sense. When you're manipulating the array, if let's say you want to add 5 to every value it contains, you need to loop within the array and add 5 to every value contained in it.
– Kevin Avignon
Nov 21 '18 at 22:26
1
Also, the variables
FilmA
andFilmB
are each declared twice. How does this compile? Anyway, I suggest that you a) Tell us exactly what the program needs to do; and b) Give us all of your code. This doesn't seem like a large program, we can take it.– Vilx-
Nov 21 '18 at 22:29
See also Best Data Type to Use for Currency --things that should be
decimal
are set todouble
(there's rounding difference with the two) , and things that would do better asint
are set todouble
. Why usefloat
instead ofint
for a single whole-number?– Symon
Nov 21 '18 at 22:33
1
You need to post a minimal complete verifiable example; the code you posted doesn't show the error you are talking about, has other errors you don't mention, and includes too much irrelevant code.
– Dour High Arch
Nov 21 '18 at 22:50