|
-
September 25th, 2007, 06:09 PM
#1
i need help making this code work
Code:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{//variable names
int Int1, Int2, Int3, sum, average, product;
//takes user input
printf("input three different integers: ");
scanf("%d%d%d", &Int1,&Int2,&Int3);
//equation process
sum = Int1 + Int2 + Int3;
average = sum / 3;
product = Int1 * Int2 * Int3;
cout<<"Enter Int1 : ";
cin>>Int1;
cout<<"Enter Int2 : ";
cin>>Int2;
cout<<"Enter Int3 : ";
cin>>Int3;
if(Int1>=Int2)
{
if(Int1>=Int3)
{
cout<<"Largest Number Is : "<<Int1;
if(Int2<=Int3)
cout<<"Smallest Number Is : "<<Int2;
else
cout<<"Smallest Number Is : "<<Int3;
}
else
{
cout<<"Largest Number Is : "<<Int3;
cout<<"Smallest Number Is : "<<Int2;
}
}
else
{
if(Int2>=Int3)
{
cout<<"Largest Number Is : "<<Int2;
if(Int1<=Int3)
cout<<"Smallest Number Is : "<<Int1;
else
cout<<"Smallest Number Is : "<<Int3;
}
else
{
cout<<"Largest Number Is : "<<Int3;
cout<<"Smallest Number Is : "<<Int1;
}
}
//prints results
printf("Sum is %d\nAverage is %d\nProduct is %d\n", sum, average, product);
return 0;
}
im trying to make it so it tells me the smallest and largest of the 3 integers but i keep getting the following error:
error C2065: 'cout' : undeclared identifier
error C2065: 'cin' : undeclared identifier
what do i do
-
September 25th, 2007, 06:34 PM
#2
Re: i need help making this code work
Code:
#include <iostream>
-
September 25th, 2007, 06:40 PM
#3
Re: i need help making this code work
 Originally Posted by Greggle
Code:
#include <iostream>
where do i put that because i was told never to touch the "stdafx.h"
-
September 25th, 2007, 06:57 PM
#4
Re: i need help making this code work
Add this to the top of your code:
Code:
#include <iostream>
using namespace std;
// etc...
Since you're using iostreams anyway, rewrite the printf and scanf calls at the beginning and end of the main function using cout and cin. Also consider not including stdafx.h, and if you don't need to support Unicode command-line arguments (chances are you don't), change _tmain to main and _TCHAR to char.
-
September 25th, 2007, 08:22 PM
#5
Re: i need help making this code work
is there a way to do it so i dont need to change printf, scanf, stdafx.h if so i would like to know
-
September 25th, 2007, 08:42 PM
#6
Re: i need help making this code work
 Originally Posted by inbugable
is there a way to do it so i dont need to change printf, scanf, stdafx.h if so i would like to know
Do you want to learn how to write a program or do you want to learn how to get flawed code to compile? In any case:
 Originally Posted by Hermit
Add this to the top of your code:
Code:
#include <iostream>
using namespace std;
// etc...
-
September 25th, 2007, 10:16 PM
#7
Re: i need help making this code work
Code:
int _tmain(int argc, _TCHAR* argv[])
{//variable names
int Int1, Int2, Int3, sum, average, product;
//takes user input
printf("input three different integers: ");
scanf("%d%d%d", &Int1,&Int2,&Int3);
//equation process
sum = Int1 + Int2 + Int3;
average = sum / 3;
product = Int1 * Int2 * Int3;
//prints results
printf("Sum is %d\nAverage is %d\nProduct is %d\n", sum, average, product);
return 0;
}
okay lets start over my assignment is to add to this so it also tells me the largest and smallest of the integers. now im not asking for it to be done for me but i know next to nothing about programming but im assuming i need to do an If statement similar to the above one but one that doesnt force me to change stdafx.h and such
-
September 25th, 2007, 10:37 PM
#8
Re: i need help making this code work
okay lets start over my assignment is to add to this so it also tells me the largest and smallest of the integers. now im not asking for it to be done for me but i know next to nothing about programming
Then take our advice.
If you want to be a programmer, you should know what is standard and non-standard.
but im assuming i need to do an If statement similar to the above one but one that doesnt force me to change stdafx.h and such
There is no such file as "stdafx.h" in standard C++. So asking us to "not touch it" doesn't make sense. To many here, it doesn't even exist, and it will just interfere with you actually writing the program correctly.
Second, the entry point function for C++ programs is main(), not tmain(). I know you use Microsoft compilers, but you should know how to write your programs, independent of whether you're using a Microsoft compiler or not.
The following is a correct C++ program that will compile under any ANSI compliant C++ compiler.
Code:
#include <cstdio>
int main()
{
int Int1, Int2, Int3, sum, average, product;
//takes user input
printf("input three different integers: ");
scanf("%d%d%d", &Int1,&Int2,&Int3);
//equation process
sum = Int1 + Int2 + Int3;
average = sum / 3;
product = Int1 * Int2 * Int3;
//prints results
printf("Sum is %d\nAverage is %d\nProduct is %d\n", sum, average, product);
return 0;
}
Some things:
Turn off precompiled headers in your project settings. Get rid of the TCHAR stuff and _tmain() so that you know what a standard C++ program consists of.
Once you know what a standard C++ program is supposed to look like, then you can go and try precompiled headers, TCHAR or whatever other Microsoft specific things you want to try.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 25th, 2007 at 10:42 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|