CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2016
    Posts
    5

    Question 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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    Oct 2016
    Posts
    5

    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.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  5. #5
    Join Date
    Oct 2016
    Posts
    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) ;
                
            }
        }
    }

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  7. #7
    Join Date
    Oct 2016
    Posts
    5

    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?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: userValue in an equation

    Quote Originally Posted by bravojohnny View Post
    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)

  9. #9
    Join Date
    Oct 2016
    Posts
    5

    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.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: userValue in an equation

    Quote Originally Posted by bravojohnny View Post
    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
    }

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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
  •  





Click Here to Expand Forum to Full Width

Featured