Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace inClass3
{
    class Program
    {
        static void Main(string[] args)
        {

            //Declare & Initalize Variables
            string firstNum = "";
            string secondNum = "";
            string thirdNum = "";
            string fourthNum = "";
            string fifthNum = "";

            //Get user input 
            Console.Write("Please enter the first number (has to be two digits): ");
            firstNum = Console.ReadLine();
            
            Console.Write("Please enter the second number (has to be two digits): ");
            secondNum = Console.ReadLine();

            Console.Write("Please enter the third number (has to be two digits): ");
            thirdNum = Console.ReadLine();

            Console.Write("Please enter the fourth number (has to be two digits): ");
            fourthNum = Console.ReadLine();

            Console.Write("Please enter the fifth number (has to be two digits): ");
            fifthNum = Console.ReadLine();


            //Convert all string inputs to double
            Console.WriteLine(Convert.ToDouble(firstNum));
            Console.WriteLine(Convert.ToDouble(secondNum));
            Console.WriteLine(Convert.ToDouble(thirdNum));
            Console.WriteLine(Convert.ToDouble(fourthNum));
            Console.WriteLine(Convert.ToDouble(fifthNum));


            //Adding first four numbers in a seperate variable
            int firstFour = int.Parse(firstNum + secondNum + thirdNum + fourthNum);

            //Add the last two numbers and stored inside a new variable 
            int lastTwo = int.Parse(firstFour + fifthNum);

            //Multiply firstFour variable & lastTwo variable and store it in a new variable 
            int multiplyTwoSums = (firstFour * lastTwo);

            //Subtract fifth number from first four variables (firstFour) 
            int allNums = int.Parse(fifthNum - firstFour); **Problem is here**





            Console.ReadKey();
        }
    }
}


What I have tried:

I've tried asking some friends but they're not sure
New to coding and tried looking at YouTube videos, textbooks, etc...
The problem is on the last line
Posted
Updated 11-Mar-22 14:08pm
v3
Comments
PIEBALDconsult 11-Mar-22 19:12pm    
Convert does not change the value of the provided parameter.
I also recommend _not_ using the Convert class.

Quote:
I've tried asking some friends but they're not sure
New to coding and tried looking at YouTube videos, textbooks, etc...
The problem is on the last line

You're looking everywhere except where the problem is. The error message is pretty explicit. You're trying to subtract an integer from a string and that just doesn't make any sense.

So, look at your code. Where are you trying to do subtraction?
C#
int allNums = int.Parse(fifthNum - firstFour);

OK, so look at where those two variables are defined:
string fifthNum = "";
and
int firstFour = int.Parse(firstNum + secondNum + thirdNum + fourthNum);

fifthNum is a string, not a number. firstFour is an integer, a number, so where's the problem? You're trying to subtract an integer from a STRING. You can't do that.

So what do you think you have to do to get the string in fifthNum into a variable of type int?

Hint: You're already doing it in other parts of your code.
 
Share this answer
 
v3
Comments
PIEBALDconsult 11-Mar-22 19:18pm    
He's doing it, but not saving the values.
Dave Kreskowiak 11-Mar-22 19:56pm    
Yep. I saw that. I concentrated more on the error message and not on the student grade quality of the code. I know that code is really bad for us to write, but making it look pro-level is just going to get the OP a failing grade.
Quote:
The problem is on the last line


Nope. The problem is everywhere. You have at least 9 lines of code that are not doing what you are hoping they would do.

1.
The operations on strings are limited mainly to concatenation, with the symbol +.
The result of "1"+"2" is not "3", it is "12"

2.
There is no - operator for strings.

3.
The Convert methods you are trying to use are real functions: they take one parameter, which they can't modify, and they return one value, which you consistently have ignored.

No, Convert.ToDouble(somestring) will not magically turn that somestring into a double; that is totally impossible in typed languages such as C#. You declared firstNum and the others as strings, so that is what they are and will continue to be throughout your Main() method.

4.
And then, your program isn't doing anything useful: it takes some inputs, attempts some calculations, and that is it. Nothing gets printed or output in any other way???


I suggest you stop watching stupid YouTube videos and start reading a good book on C#.
 
Share this answer
 
v3
I would try to replae
C#
int allNums = int.Parse(fifthNum - firstFour);

with
C#
int allNums = int.Parse(fifthNum) - firstFour;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900