0

I am very new to batch, and I have this program that is SUPPOSED to take data in then echo the output of the data, but I need help on making a varible from an equation in cmd. Here is the code to my program. Could anybody tell me what i am doing wrong here? Thanks!

@echo off
title growth factor y varible tool
set/p yintercept=enter the y intercept
echo %yintercept%
set/p exponent=enter the exponent
echo %exponent%
set/p x=enter x value
echo %x%
cls
echo %yintercept%
echo %exponent%
echo %x%
eq = %yintercept% ( %exponent% ^ %x% )
pause >nul
unclemeat
  • 5,029
  • 5
  • 28
  • 52
  • To correct the code layout, put 4 spaces before each line; you can see what I did in my edit. – Jonathan Apr 16 '15 at 00:10
  • Please see [this page](http://stackoverflow.com/help/how-to-ask) for help with asking a good question. For help with correct formatting, see [this page](http://stackoverflow.com/help/formatting). – unclemeat Apr 16 '15 at 00:23

2 Answers2

1

Here is a working sample of your code:

@echo off
title Growth factor y varible tool
set /p yin="Enter the y intercept: "
set /p exp="Enter the exponent: "
set /p x="Enter x value: "
pause
cls
echo Y-intercept:   %yin%
echo Exponent:      %exp%
echo X value:       %x%
echo.
set /a pow=1
for /l %%a in (1, 1, %x%) do set /a pow*=exp
set /a eq=yin*pow
echo Equation = %yin% * (%exp%^^%x%)
Echo          = %yin% * %pow%
Echo          = %eq%
pause >nul

If you input:

Enter the y intercept: 2
Enter the exponent: 3
Enter x value: 4
Press any key to continue . . .

It outputs:

Y-intercept:   2
Exponent:      3
X value:       4

Equation = 2 * (3^4)
         = 2 * 81
         = 162

And as you can see includes a bit of working. You can easily get rid of this by commenting the respective lines.

Monacraft
  • 6,510
  • 2
  • 17
  • 29
0

Firstly, you have not used the set command when trying to evaluate %yintercept% ( %exponent% ^ %x% ). You need to use set with the /a switch for arithmetic.

However, it looks like you are trying to evaluate %exponent% to the power of %x%. There is no operator to do this with the set command. See this question for an explanation on how to do this in Batch. Note: in CMD the set command evaluates carat symbol ^ as bitwise exclusive or, and since carat is used as an escape character, you would need to encase this expression in quotation marks, or add a second carat.

Also, The set command does not imply multiplication symbols. So you need to include * where you would like multiplication to occur.

Type set /? into a command prompt for an explanation on how to use the set command, specifically the section on the /a switch.

Community
  • 1
  • 1
unclemeat
  • 5,029
  • 5
  • 28
  • 52