adding variables with equations

This is a discussion on adding variables with equations within the C++ Programming forums, part of the General Programming Boards category; hi, i'm having trouble correcting the syntax errors in this program it should except input from the keyboard for the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    adding variables with equations

    hi, i'm having trouble correcting the syntax errors in this program it should except input from the keyboard for the first and second equations:
    for example,
    3a+9b+10c-8d=6
    9a-2b+3c-4d=4
    and the sum and difference should be the output. i appreciate any feedback how to correct this program.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() 
    { 
    	
    	int i1,
    		i2,
    		sum,
    		difference;
    	cout<<"Input the top equations: ";
    	cin>>i1;
    	cout<<"Input the bottom equations: ";
    	cin>>i2;
    	sum=i1+i2;
    	difference=i1-i2;
    	cout<<endl;
    	cout<<"The sum of "<<i1<<" and "<<i2<<" is "<<sum<<endl;
    	cout<<"The difference of "<<i1<<" and "<<i2<<" is "<<difference<<endl;
    	return 0;
    }
    Code:
    1>------ Rebuild All started: Project: complex, Configuration: Debug Win32 ------
    1>Deleting intermediate and output files for project 'complex', configuration 'Debug|Win32'
    1>Compiling...
    1>complex.cpp
    1>c:\users\martin\documents\visual studio 2008\projects\complex\complex\complex.cpp(30) : error C2059: syntax error : '}'
    1>c:\users\martin\documents\visual studio 2008\projects\complex\complex\complex.cpp(30) : error C2143: syntax error : missing ';' before '}'
    1>c:\users\martin\documents\visual studio 2008\projects\complex\complex\complex.cpp(30) : error C2059: syntax error : '}'
    1>Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\complex\complex\Debug\BuildLog.htm"
    1>complex - 3 error(s), 0 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I think you have bigger problems than the syntax at the minute. Are you hoping that when this compiles, you are able to enter an equation into an integer?

    I think you need to look into data types, and I would also say to look at functions for calculating the equations such as:

    Code:
    double (a, b, c){
    //calculation
    //return result
    }

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    The only way I know of to "except input from the keyboard" would be to:
    Code:
    cin.tie(stdnull);
    Sorry, eager helper left for work this morning but SmatA$$ came home..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    The portion of source code you provided is not even 30+ lines long, so it's hard for us to tell where line 30 is. That's where your errors are, but there's no way for us to tell which line(s) of code are the problem here. And I can't see those errors anywhere in the code you provided.

    Also, unless you have an option set somewhere in your IDE, that console window will disappear before you're able to see anything, so add a "cin.get();" statment before the "return 0;" statement, which requires 'enter' to be pressed before closing the window. Actually you will probably need 2 "cin.get()" statements since something is already there from the first call to "cin <<".

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by jrohde View Post
    The portion of source code you provided is not even 30+ lines long, so it's hard for us to tell where line 30 is. That's where your errors are, but there's no way for us to tell which line(s) of code are the problem here. And I can't see those errors anywhere in the code you provided.

    Also, unless you have an option set somewhere in your IDE, that console window will disappear before you're able to see anything, so add a "cin.get();" statment before the "return 0;" statement, which requires 'enter' to be pressed before closing the window. Actually you will probably need 2 "cin.get()" statements since something is already there from the first call to "cin <<".
    I imagine he probably has another source file open within the same project with a main function also. That is probably where the syntax errors are coming from. Not sure if all IDE's do this but I know VC++ does.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    1
    3a+9b+10c-8d=6
    9a-2b+3c-4d=4
    The sum equation result is:
    12a + 7b +13c -12d = 10

    But I guess he doesn't know how to add two equations to get into the result line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  2. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  3. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 07:34 PM
  4. LUP Decomposition (simultaneous equations)
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 03:08 AM
  5. Solving linear equations
    By PJYelton in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2002, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21