-
October 27th, 2016, 05:40 AM
#1
userValue in an equation
I know this may be c# 101-level stuff, but I can't seem to find an answer. I'm trying to use a userValue in a formula that subtracts 6 in a loop until It gets an int value between 1 & 6. Nothing I try seems to work. .Net v4.5
-
October 27th, 2016, 05:45 AM
#2
Re: userValue in an equation
It would be helpful if you would post the code you have so that we can have a look at it. When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 27th, 2016, 07:00 PM
#3
Re: userValue in an equation
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number.");
string userValue = Console.ReadLine();
for (int userValue; userValue > 0; userValue -= 6) ;
}
This is what I have so far. It should be ridiculously easy, but I can't seem to wrap my head around it.
-
October 28th, 2016, 02:07 AM
#4
Re: userValue in an equation
You are reading userValue as a string, but you need it as a number. Also you are defining an int userValue in the for loop but this userValue isn't the same variable as the string userValue. To convert a string to an int see https://msdn.microsoft.com/en-us/library/bb397679.aspx
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 8th, 2016, 09:37 PM
#5
Re: userValue in an equation
What else am I missing? I'm still getting the red error lines.
Code:
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your circuit Number.");
string userValue = Console.ReadLine();
int x = Convert.ToInt32(userValue);
for (int x > 0; x -= 6) ;
}
}
}
-
November 9th, 2016, 02:29 AM
#6
Re: userValue in an equation
Code:
int x = Convert.ToInt32(userValue);
for (int x > 0; x -= 6) ;
for statement takes 3 arguments separated by ;. In this case the initialise part of the for is missing. Try
Code:
for (int x = Convert.ToInt32(userValue); x > 0; x -= 6) ;
Note that this will not actually do much as there is no statements in the body of the for loop.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 14th, 2016, 07:46 PM
#7
Re: userValue in an equation
So how do I get it to do something, namely, take the number from the user and continuously subtract it by 6 until it reaches to between 1 & 6?
-
November 15th, 2016, 02:42 AM
#8
Re: userValue in an equation
Originally Posted by bravojohnny
So how do I get it to do something, namely, take the number from the user and continuously subtract it by 6 until it reaches to between 1 & 6?
Code:
int x = Convert.ToInt32(userValue);
for (; x > 6; x -= 6) ;
and then what? x is now between 1 & 6 assuming x was > 0 to start with.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 16th, 2016, 08:52 PM
#9
Re: userValue in an equation
The whole point of the code is to take an input number from a user and subtract 6 from it until it's between 1 & 6. That's why I have the lines
Code:
Console.WriteLine("Enter your circuit Number.");
string userValue = Console.ReadLine();
so
Code:
for (int x = Convert.ToInt32(userValue); x > 0; x -= 6) ;
can take the uservalue and do its subtraction loop.
-
November 17th, 2016, 02:33 AM
#10
Re: userValue in an equation
Originally Posted by bravojohnny
The whole point of the code is to take an input number from a user and subtract 6 from it until it's between 1 & 6. That's why I have the lines
Code:
Console.WriteLine("Enter your circuit Number.");
string userValue = Console.ReadLine();
so
Code:
for (int x = Convert.ToInt32(userValue); x > 0; x -= 6) ;
can take the uservalue and do its subtraction loop.
Sure, it loops, but it doesn't do anything other than loop. If you want it to do something, code it like this (and replace the comment with some code):
Code:
for (int x = Convert.ToInt32(userValue); x > 0; x -= 6)
{
// Put code that does something here
}
-
November 17th, 2016, 06:54 AM
#11
Re: userValue in an equation
Why not something simple like this instead of a loop - unless you want to undertake processing for each loop iteration?
Code:
Console.WriteLine("Enter your circuit Number.");
string userValue = Console.ReadLine();
int x = ((Convert.ToInt32(userValue) - 1) % 6 ) + 1;
//x now has a value between 1 and 6 assuming circuit number read was > 0
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|