C
C#2y ago
0_00

build errors in my function

i want to call the function until the loop ends but its giving me build errors that I'm not 100% sure how to fix.
39 Replies
jcotton42
jcotton422y ago
post the entire file $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
0_00
0_00OP2y ago
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0);
{
Console.WriteLine("Ammount due: {0}", i);
coinInteraction(i);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i){

Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
return false;
}



}
}
}
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0);
{
Console.WriteLine("Ammount due: {0}", i);
coinInteraction(i);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i){

Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
return false;
}



}
}
}
sent
jcotton42
jcotton422y ago
and which line is line 15?
0_00
0_00OP2y ago
Console.WriteLine("Change due: {0}", Math.Abs(i));
Console.WriteLine("Change due: {0}", Math.Abs(i));
jcotton42
jcotton422y ago
a couple things - you have a ; at the end of the while line, I don't think you wanted that - coinInteraction has a float return type, but you're returning false, which is a bool also, when I paste this into sharplab I get different errors have you saved the file you're looking at?
0_00
0_00OP2y ago
yeah the file is saved
jcotton42
jcotton422y ago
well the compiler isn't seeing the same file you are so either it's not saved, or you're editing a different file
0_00
0_00OP2y ago
i think it was a saving issue
0_00
0_00OP2y ago
0_00
0_00OP2y ago
so with the bool to float, what I want with that block is to have it go back to the top of the loop like nothing happened should i just have it break instead?
jcotton42
jcotton422y ago
that's what continue does, break ends the loop but only inside the while body, inside of functions called by while it won't work
0_00
0_00OP2y ago
ok so I have updated my code
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0)
{
Console.WriteLine("Ammount due: {0}", i);
Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
continue;
}
coinInteraction(i);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i, int coininserted){
coinInteraction = i - coininserted;
}
}
}
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0)
{
Console.WriteLine("Ammount due: {0}", i);
Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
continue;
}
coinInteraction(i);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i, int coininserted){
coinInteraction = i - coininserted;
}
}
}
so now the while loop should work with my if statement
jcotton42
jcotton422y ago
coinInteraction = i - coininserted; as coinInteraction is a method, this makes no sense
0_00
0_00OP2y ago
}
coinInteraction(ongoingbalance);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i, int coininserted){
ongoingbalance = i - coininserted;
}
}
}
}
coinInteraction(ongoingbalance);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i, int coininserted){
ongoingbalance = i - coininserted;
}
}
}
still doesnt run but this should be on the right track?
jcotton42
jcotton422y ago
that's not how returning from methods works you use return
0_00
0_00OP2y ago
public static float coinInteraction(int i, int coininserted){
ongoingbalance = i - coininserted;
return ongoingbalance;
}
public static float coinInteraction(int i, int coininserted){
ongoingbalance = i - coininserted;
return ongoingbalance;
}
jcotton42
jcotton422y ago
why the assignment? just return the expression also, if you tried to compile that, you would see the compiler complain about assigning to an undeclared variable
0_00
0_00OP2y ago
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0)
{
Console.WriteLine("Ammount due: {0}", i);
Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
continue;
}
coinInteraction(i, coininserted);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i, int coininserted){
return i - coininserted;
}
}
}
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0)
{
Console.WriteLine("Ammount due: {0}", i);
Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
continue;
}
coinInteraction(i, coininserted);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static float coinInteraction(int i, int coininserted){
return i - coininserted;
}
}
}
ok so now I have logic issues because the user input does not update i
0_00
0_00OP2y ago
0_00
0_00OP2y ago
it just keeps looping and doesnt use the coinInteraction method
jcotton42
jcotton422y ago
yes it is but you're not doing anything with the return value
0_00
0_00OP2y ago
so i want the return value to replace the i value in my while loop but its not as simple as setting my method to i
jcotton42
jcotton422y ago
how is it not? what does "setting my method to i" mean to you? b/c I think we have different understandings of that
0_00
0_00OP2y ago
something like
i = coinInteraction(i, coininserted)
i = coinInteraction(i, coininserted)
jcotton42
jcotton422y ago
and why doesn't that work?
0_00
0_00OP2y ago
0_00
0_00OP2y ago
ok wait my method is set as a float on line 25 would that be why im getting an error ok so i changed line 25 from float to int and it runs again but with the same logic issue
0_00
0_00OP2y ago
0_00
0_00OP2y ago
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0)
{
Console.WriteLine("Ammount due: {0}", i);
Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
continue;
}
i = coinInteraction(i, coininserted);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static int coinInteraction(int i, int coininserted){
return i - coininserted;
}
}
}
using System;

namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int i=50;
while (i > 0)
{
Console.WriteLine("Ammount due: {0}", i);
Console.WriteLine("Insert a Coin");
string coininsert = Console.ReadLine();
int coininserted = Int32.Parse(coininsert);
if (coininserted != 5 || coininserted != 10 || coininserted != 25){
continue;
}
i = coinInteraction(i, coininserted);
if (i <= 0){
Console.WriteLine("Change due: {0}", Math.Abs(i));
}
}
}
public static int coinInteraction(int i, int coininserted){
return i - coininserted;
}
}
}
the updated code
jcotton42
jcotton422y ago
if (coininserted != 5 || coininserted != 10 || coininserted != 25) this will always be true
0_00
0_00OP2y ago
how can i make that only true when those values arent inputted because shouldnt it not be true if the input is 5, 10, or 25
jcotton42
jcotton422y ago
think about it why it's always true plug a few values into that expression, see what comes out (like in your head or on paper)
0_00
0_00OP2y ago
tbh im kind of stumped, my only guess would be the Int32.Parse on line 15
jcotton42
jcotton422y ago
no, it's that if statement specifically that's the problem and more specifically, its condition coininserted != 5 || coininserted != 10 || coininserted != 25 think about what happens when you plug 5 into that or 6, or 7
0_00
0_00OP2y ago
yeah ok nvm i see it now even if my value is one of the values i want it wont get passed the other two checks ?
jcotton42
jcotton422y ago
yes you meant &&, not ||
0_00
0_00OP2y ago
it works perfectly now thanks so much for your help appreciate you explaining everything to me
jcotton42
jcotton422y ago
np
Want results from more Discord servers?
Add your server