As the title says, more or less. I'm just trying to make a program basically (Variable) + (Variable) = (Total)

Here is the script:

// Variable + Variable = Total.cpp : main project file.

#include "stdafx.h"

using namespace System;

void main()
{
    float num1;
    float num2;
    float total;

    cout<<"enter a value for the first variable:";
    cin>> num1;
    cout<<"enter a value for the second variable:";
    cin>> num2;

    total = num1 + num2;

    cout<<"The sum of the numbers =" << total;
}

I think that #include "stdafx.h" is a template from the IDE that I'm using (Microsoft Visual C++ 2008 Express Edition)

Here is the error that I get when I try to compile it...:

------ Build started: Project: Variable + Variable = Total, Configuration: Debug Win32 ------
Compiling...
Variable + Variable = Total.cpp
.\Variable + Variable = Total.cpp(13) : error C2065: 'cout' : undeclared identifier
.\Variable + Variable = Total.cpp(14) : error C2065: 'cin' : undeclared identifier
.\Variable + Variable = Total.cpp(15) : error C2065: 'cout' : undeclared identifier
.\Variable + Variable = Total.cpp(16) : error C2065: 'cin' : undeclared identifier
.\Variable + Variable = Total.cpp(20) : error C2065: 'cout' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Variable + Variable = Total\Variable + Variable = Total\Debug\BuildLog.htm"
Variable + Variable = Total - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any help is appreciated, again take into account that I really don't have any clue what I'm doing, This is like my first week or so in CPP, so bare with me.

Recommended Answers

All 8 Replies

You need to include<iostream> this is where the cin and cout are found. Also change void main to int main. A main function must always return a value. You need to also change

using namespace system

to

using namespace std;

You need to include the file which declares cout,cin i.e. #include <iostream> and namespace 'std'. i dont think you need the stdafx.h at all.

and please read the welcome guide to understand the posting rules and regulations.

Got it, I'll definitly check out the rules & regulations right after this. Here is the error message I got after changing the script:


------ Build started: Project: Test 2, Configuration: Debug Win32 ------
Compiling...
Test 2.cpp
.\Test 2.cpp(3) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header
.\Test 2.cpp(22) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Test 2\Test 2\Debug\BuildLog.htm"
Test 2 - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Did you remove the stdafx.h? post your updated code here so we can see what you have done

Did you remove the stdafx.h? post your updated code here so we can see what you have done

Yes he did.

It's important to #include "stdafx.h" at the very beginning of your program.

#include "stdafx.h"
#include <iostream>

using namespace std; 
//etc

If you don't want to use the precompiled header, you can uncheck the option "use precompiled header" when creating a project.

Awesome, That seemed to solve it. One last question - When I debug it, what do I do to have it stay up (rather than adding Variable + Variable then getting total and closing)

Hope that made sense.

You just need to add cin.get(); at the bottom, right before the closing {

Awesome, works as it should now. Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.