C
C#

help

build errors in my function

0_002/4/2023
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.
Jjcotton422/4/2023
post the entire file $code
MMODiX2/4/2023
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_002/4/2023
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
Jjcotton422/4/2023
and which line is line 15?
0_002/4/2023
Console.WriteLine("Change due: {0}", Math.Abs(i));
Console.WriteLine("Change due: {0}", Math.Abs(i));
Jjcotton422/4/2023
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_002/4/2023
yeah the file is saved
Jjcotton422/4/2023
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_002/4/2023
i think it was a saving issue
0_002/4/2023
0_002/4/2023
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?
Jjcotton422/4/2023
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_002/4/2023
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
Jjcotton422/4/2023
coinInteraction = i - coininserted; as coinInteraction is a method, this makes no sense
0_002/4/2023
}
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?
Jjcotton422/4/2023
that's not how returning from methods works you use return
0_002/4/2023
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;
}
Jjcotton422/4/2023
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_002/4/2023
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_002/4/2023
0_002/4/2023
it just keeps looping and doesnt use the coinInteraction method
Jjcotton422/4/2023
yes it is but you're not doing anything with the return value
0_002/4/2023
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
Jjcotton422/4/2023
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_002/4/2023
something like
i = coinInteraction(i, coininserted)
i = coinInteraction(i, coininserted)
Jjcotton422/4/2023
and why doesn't that work?
0_002/4/2023
0_002/4/2023
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_002/4/2023
0_002/4/2023
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
Jjcotton422/4/2023
if (coininserted != 5 || coininserted != 10 || coininserted != 25) this will always be true
0_002/4/2023
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
Jjcotton422/4/2023
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_002/4/2023
tbh im kind of stumped, my only guess would be the Int32.Parse on line 15
Jjcotton422/4/2023
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_002/4/2023
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 ?
Jjcotton422/4/2023
yes you meant &&, not ||
0_002/4/2023
it works perfectly now thanks so much for your help appreciate you explaining everything to me
Jjcotton422/4/2023
np

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ View cannot be found.LoginModel.cs Pages/Account/Login.cshtml Pages/Account/Register.cshtml AccountController.cs attache❔ Unsupported Media Type - Razor page```{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","st✅ JsonConvert debugging helpHello! I'm having a hard time figuring out how to solve an issue where JsonConvert.DeserializeObject❔ Make a sprite follow mouse while pivoting around character in UnityHi, I its hard to describe my issue by describing it since im not sure exactly what I'm looking for ❔ is Generic Repository a bad implementation of Repository Pattern?Surfing the internet I have found several articles that say it is an anti-pattern because it is an a❔ Problems with using Dotnet 6 on Windows to publish to an exe formatDotnet/C# newb here. Previously I've only done a small amount of development using the **.Net Framew❔ Using net7 documentation while targeting netstandard2.0This might be a bit of a dumb question, but is there anyway to make omnisharp/roslyn's tooltips use ❔ WPF app unit testingI am learning how to make unit tests. I built my app on .net core 6.0 and I created class library (o❔ where exactly should i put authorizationso i read a few posts on where to put authorization and most of the people agree on putting it in th❔ C# WPF playing background musicHello, I have WPF app for game launcher/updater. I wanted to add background music, so I added this cC# wpf mvvm Datagrid filter using LINQSo here I have a MVVM form. the Form contains a Datagrid which is connected to the Databank. I also ❔ Is it worth to create lazy dictionary in this case?I have following class: so as you see Add function add to the dictionary class TreeNode, it looks li❔ Going to use a listbox with a switch for something in the morning, need help with format for bothI would like to set two variables with a listbox using a switch and one variable with a second listb✅ What's your view on tool like Sonarqube?Should I learn from the code smells it captures?✅ Autodesk Maya HelpAnyone in here know Autodesk Maya really well? My mesh tool isn't letting my polygon primitives mesh❔ circular primesso this program is meant to find how many circular primes there are below 1 million (should be 55), ❔ WPF Cannot find child when it's thereHi! I am working on my own RoundedCorners attachedproperty however I have stumbled upon a problem Pr❔ HttpClient exiting program during GetStringAsync() and GetAsync()I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out ✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray() ❔ force a ushort value?if i type for example var num = 25L; i am forcing a long value (int64) how can i force a ushort in