Click here to Skip to main content
15,888,157 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've been trying to write some code in visual studio's 2015 applications folder for a mini project. The aim of the task is to create a calculator for the heat conduction equation.

After managing to create a working calculator using the basics, I was then told to try and add a message box for when the input values of the calculator are invalid.

I did this by equating the input values to -1.0E-99 unless otherwise stated, however, when I tried to add in a message box statement the curly bracket before it (line 37) came up with an error reading "} expected".

After looking it up and adding curly brackets everywhere in the code to try and get rid of it, I'm still at a loss.

Does anyone please have a solution for me? I would greatly appreciate any advice you might have.

Thank you very much and many kind regards

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Heat_conduction_calculator_9
{
    public partial class Form1 : Form
    {
        double A, q, -k, T1, T2, x1, x2, Z;

        public Form1()
        {
            InitializeComponent();

            Z = -1.0E-99;
                -k = A = T1 = T2 = x1 = x2 = Z;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            x1 = double.Parse(textBox1.Text);
            T1 = double.Parse(textBox2.Text);
            x2 = double.Parse(textBox3.Text);
            T2 = double.Parse(textBox4.Text);
            k = double.Parse(textBox5.Text);
            A = double.Parse(textBox6.Text);
            if ((A >= Z) && (-k >= Z) && (x1 >= Z) && (x2 >= Z) && (T1 >= Z) && (T2 >= Z)) ;
            {
                q = (-1 * k * A * (T2 - T1) / (x2 - x1));
                textBox7.Text = q.ToString();
} //-----> line 37
            else if ((A <= Z)|(-k <= Z)|(x1 <= Z)|(x2 <= Z)|(T1 <= Z)|(T2 <= Z));
            MessageBox.Show("Invalid Input");

        }
                
    }
}
Posted
Updated 18-Mar-17 10:42am
v2
Comments
Bryian Tan 18-Mar-17 14:11pm    
By the way, was that a typos on the variable name -k ?
[no name] 18-Mar-17 14:27pm    
Take the - off of k declaration and get rid of the ; at the end of your if statement.

C++
            if ((A >= Z) && (-k >= Z) && (x1 >= Z) && (x2 >= Z) && (T1 >= Z) && (T2 >= Z)) ;
            {
                q = (-1 * k * A * (T2 - T1) / (x2 - x1));
                textBox7.Text = q.ToString();
} //-----> line 37
            else if ((A <= Z)|(-k <= Z)|(x1 <= Z)|(x2 <= Z)|(T1 <= Z)|(T2 <= Z));
            MessageBox.Show("Invalid Input");

The 2 ";" at the end of the if line are mistakesn they are not C/C++, remove them.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
Comments
Member 13068046 21-Mar-17 19:15pm    
You were right, the moment I removed them everything worked. Thank you so much!
You have two problems:
You can't name a variable -k. If you want it to be the negative of Z you just have to name it K and set it to -Z.
Secondly if statements don't have a ; at the end of them. That is effectively ending your if statment. Here is code that should do what you want:

C#
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Heat_conduction_calculator_9
{
    public partial class Form1 : Form
    {
        double A, q, k, T1, T2, x1, x2, Z;

        public Form1()
        {
            InitializeComponent();

            Z = -1.0E-99;
            A = T1 = T2 = x1 = x2 = Z;
            k = -Z;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            x1 = double.Parse(textBox1.Text);
            T1 = double.Parse(textBox2.Text);
            x2 = double.Parse(textBox3.Text);
            T2 = double.Parse(textBox4.Text);
            k = double.Parse(textBox5.Text);
            A = double.Parse(textBox6.Text);
            if ((A >= Z) && (-k >= Z) && (x1 >= Z) && (x2 >= Z) && (T1 >= Z) && (T2 >= Z)) 
            {
                q = (-1 * k * A * (T2 - T1) / (x2 - x1));
                textBox7.Text = q.ToString();
            } //-----> line 37
            else if ((A <= Z) | (-k <= Z) | (x1 <= Z) | (x2 <= Z) | (T1 <= Z) | (T2 <= Z)) 
            MessageBox.Show("Invalid Input");

        }

    }
}
 
Share this answer
 
Comments
Member 13068046 21-Mar-17 19:14pm    
Thank you very much! I didn't spot that.

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