PDA

View Full Version : Creating a Basic Mathmatic Program in C++



Hazzy
2008-07-03, 10:30 AM
Well, I want to write a program to do a pretty simple function, using Visual C++ Microsoft 2008 Edition. However, first, I wanted to get the hang of what needs to be done to get any program to work, so I took one out of my book,

bool accept ()
{
cout << "Do you want to proceed (y or n)?\b";

char answer = 0;
cin >> answer;

switch (answer) {
case 'y':
return true;
case 'n':
return false;
default:
count << "I'll take that for a no. /n"
return false;
}
}

I typed it up in a new *.cpp file, and tried to debug. The debug option was grayed out, so I'm thinking I need to do more than just type up a file.
So, my question, how exactly do I get a simple program to work? D:
I have no idea how C++ works, lol, as this is my first real attempt at trying to make a program.

Please and thank you for your time. :D
*Is half away at 9:30 in the morning and has made weird typos like spelling "done" as "dun*

~~~~~~~~~~~~~
I tried compiling with the Command Prompt, that's what I got from the Help tab, and got these errors.

C:\Program Files\Microsoft Visual Studio 9.0\vc\bin>cl /EHsc BoolAccPro.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

BoolAccPro.cpp
BoolAccPro.cpp(3) : error C2065: 'cout' : undeclared identifier
BoolAccPro.cpp(6) : error C2065: 'cin' : undeclared identifier
BoolAccPro.cpp(14) : error C2065: 'count' : undeclared identifier
'return'


How do I declare the identifiers? ._.

Spaz
2008-07-03, 08:19 PM
Looks like you're missing


#include <iostream>
using namespace std;

You'll also need a main function.

x0xpriestx0x
2008-07-09, 03:41 PM
maybe you should try to learn the most easiest program first. Here is the most simplest program that typed out "Hello World!"

#include <iostream.h>

main()
{
for(;;)
{
cout << "Hello World! ";
}

}

or if i remember correctly it'll also work if you just have
#include <iostream.h>

main()
{
cout << "Hello World! ";
}


Here is the link to that program.
http://www2.latech.edu/~acm/helloworld/c++.html

You would also need to go read the beginners book or maybe try C++ for dummies first. You can't just learn how to program without reading a book.

Conciente
2008-07-09, 03:59 PM
All programs have the following:


#include <stdio.h>
#include <stdlib.h>

int main()
{

system("pause");
return 0;
}

To declare the identifiers you do the following:


data_type variable_name;

Example:

int a;
float b;
char c;


If you want to make a simple math equation, like adding two numbers, here's an example:


#include <stdio.h>
#include <stdlib.h>

int main()
{
int a, b, c; //declaring the variables

printf("Insert the first number: "); //asking the user to input the number
scanf("%d", &a); //saving that number on the variable "a"

printf("Insert the second number: "); //see above
scanf("%d", &b); //see above

c=a+b; //self-explanatory.

printf("The result is: %d ", c); //the user will get the answer.

system("pause");
return 0;
}