Posted: Tue Jun 10, 2008 3:29 pm Post subject: won't add sum of an array
var x : int
%integer variable for number of guests
var pizzatype : string
var totslices : int
loop
cls
put "Please enter number of guests: "
get x
put "Please enter your type of pizza (pepperoni, vegetarian or hawaiian"
get pizzatype
var slices : array 1 .. x of int
%creates an array for asking for the number of slices for all the guests
if pizzatype = "pepperoni" then
for i : 1 .. upper (slices)
put "Please enter how many slices you would like of pepperoni pizza, person", i
get slices (i)
totslices += slices (i)
end for
put totslices
end if
if pizzatype = "vegetarian" then
for i : 1 .. upper (slices)
put "Please enter how many slices you would like of vegetarian pizza, person", i
get slices (i)
totslices += slices (i)
end for
put totslices
end if
if pizzatype = "hawaiian" then
for i : 1 .. upper (slices)
put "Please enter how many slices you would like of hawaiian pizza, person", i
get slices (i)
totslices += slices (i)
end for
put totslices
end if
end loop
I can't seem to get totslices to set equal to the sum of the slices array and then output the sum. Any ideas?
Sponsor Sponsor
gitoxa
Posted: Tue Jun 10, 2008 3:33 pm Post subject: RE:won\'t add sum of an array
You need to initialize "totslices"
And just curious, why is the number of guests variable "x" ? The other ones make sense, but not that one
andrew.
Posted: Tue Jun 10, 2008 6:28 pm Post subject: RE:won\'t add sum of an array
Initialize "totslices" like gitoxa said.
To do that, just add := 0 after the declaration (var totslices....).
coreyxjessica
Posted: Tue Jun 10, 2008 6:48 pm Post subject: Re: won't add sum of an array
Yeah I figured that out, I felt so dumb once I figured it out. I'm so used to the default value of an integer being 0. Oh well, this is how one learns I guess.
coreyxjessica
Posted: Tue Jun 10, 2008 8:19 pm Post subject: Re: RE:won\'t add sum of an array
gitoxa @ Tue Jun 10, 2008 3:33 pm wrote:
You need to initialize "totslices"
And just curious, why is the number of guests variable "x" ? The other ones make sense, but not that one
x was just a placeholder, I like to have working code before I change things I'm meaning to change. I really don't know why.