PDA

View Full Version : if you don't have visual studio 2010 don't even come here



Bandito
02-18-2014, 10:48 PM
// cap2 2.19.cpp : Defines the entry point for the console application.
//Este programa calcula la suma, resta, multiplicacion, promedio, el mas pequeno y el mas grande de tres numeros

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int n1, n2, n3, sum, sub, mult, ave, smallest, largest;//Inicializar variables
cout<<"Input three integers ";
cin>>n1>>n2>>n3;//Numeros que el usuario anade
sum = n1+n2+n3;//suma de los numeros
sub = n1+n2+n3;//resta de los numeros
mult = n1*n2*n3;//multiplicacion de los numeros
ave = sum/3;// este es los calculos del promedio
if (n1>n2)//Este if statement me calcula cual es el mas grande de los numeros
if(n1>n3)
largest = n1;
else
if (n2>n3)
largest = n2;
else
largest = n3;

if (n1<n2)//este if statement me calcula el mas pequeno
if(n1<n3)
smallest = n1;
else
if (n2<n3)
smallest = n2;
else
smallest = n3;
system("cls");
cout<<"Input three integers "<<n1<<" "<<n2<<" "<<n3<<endl;
cout<<"Sum is "<<sum<<endl<<"Average is "<<ave<<endl<<"Product is "<<mult<<endl<<"Smallest is "<<smallest<<endl<<"Largest is "<<largest<<endl;
system("pause");
return 0;
}



This is a code from a book I am studying from. Well for some reason it says the largest variable is not initialized when it is. I don't know where I am messing up. Can somebody here help me? It was programmed in C++....

JohnFreeman
02-18-2014, 10:51 PM
Not sure don't have it.

outbreak
02-18-2014, 10:52 PM
you've messed up the syntax

Bandito
02-18-2014, 10:57 PM
you've messed up the syntax
can you tell me where? I am so lost.

Bandito
02-18-2014, 10:57 PM
Not sure don't have it.
then why did you come here and post?:biggums:

JohnFreeman
02-18-2014, 11:00 PM
then why did you come here and post?:biggums:
Thought I would say hi

Bandito
02-18-2014, 11:04 PM
Thought I would say hi
:cheers:

shaq2000
02-18-2014, 11:12 PM
Firstly, even though I suppose those are considered single statements, I would recommend using braces with nested conditionals like that.

About the uninitialized 'largest' variable, if n1 is smaller than n2 then 'largest' is never initialized. And you're forgetting about equality. What if n1 and n2 are equal? Neither 'largest' nor 'smallest' will be set.

Setting them to 0 when you declare them will fix your run-time error:


int n1, n2, n3, sum, sub, mult, ave, smallest = 0, largest = 0;

but you've still got that logic error and you won't always be getting correct output.

CelticBaller
02-18-2014, 11:15 PM
You can't tell me how to live my life!

Bandito
02-18-2014, 11:19 PM
[QUOTE=shaq2000]Firstly, even though I suppose those are considered single statements, I would recommend using braces with nested conditionals like that.

About the uninitialized 'largest' variable, if n1 is smaller than n2 then 'largest' is never initialized. And you're forgetting about equality. What if n1 and n2 are equal? Neither 'largest' nor 'smallest' will be set.

Setting them to 0 when you declare them will fix your run-time error:


int n1, n2, n3, sum, sub, mult, ave, smallest = 0, largest = 0;

but you've still got that logic error and you won't always be getting correct o


EDIT: OH I really see what you are saying after I saw the code again. Is not going into the IF because n1 > n2 is not being satisfied:facepalm:

I ahve to go into the thinking pad again...

sixerfan82
02-18-2014, 11:26 PM
As another person mentioned, you should properly initialize your variables, which is one of a few issues I see.

It's the details that are missing, and those can be fixed.

For example, you can complete your initializing by simple adding "= 0" after each of your variables, I would recommend putting each on a separate line to improve readability.

Secondly, the lack of braces in your conditional logic is breaking it.

Let's take the following example..

if (n1>n2)//Este if statement me calcula cual es el mas grande de los numeros
if(n1>n3)
largest = n1;
else
if (n2>n3)
largest = n2;
else
largest = n3;

To make sure these nested conditions are properly executed, you should use braces such as..


if (n1>n2)//Este if statement me calcula cual es el mas grande de los numeros
{
if(n1>n3)
{
largest = n1;
}
else
{
if (n2>n3)
{
largest = n2;
}
else
{
largest = n3;
}
}
}


By default, with you omit braces such as { }, only the first line of code after an if -or- and else is executed, however, when braces are used, you can execute multiple lines of code within a conditional.

Braces { }, 'house' blocks of code and can be attached to loops, condition statements, or even functions, such as your 'main' function, notice it's braces?

shaq2000
02-18-2014, 11:35 PM
If you're allowed to use the handy C++ min/max functions:


largest = max(n1, max(n2, n3));
smallest = min(n1, min(n2, n3));

if you're not:


largest = (n1 >= ((n2 >= n3) ? n2 : n3) ) ? n1 : ((n2 >= n3) ? n2 : n3);
smallest = (n1 <= ((n2 <= n3) ? n2 : n3) ) ? n1 : ((n2 <= n3) ? n2 : n3);

Bandito
02-18-2014, 11:40 PM
If you're allowed to use the handy C++ min/max functions:


largest = max(n1, max(n2, n3));
smallest = min(n1, min(n2, n3));

if you're not:


largest = (n1 >= ((n2 >= n3) ? n2 : n3) ) ? n1 : ((n2 >= n3) ? n2 : n3);
smallest = (n1 <= ((n2 <= n3) ? n2 : n3) ) ? n1 : ((n2 <= n3) ? n2 : n3);
:cheers:

I am going by the book and they haven't talked about functions. Trying to get ahead of the class...

Draz
02-18-2014, 11:42 PM
Came to say hello

shaq2000
02-18-2014, 11:45 PM
Also, your average variable should be a float or double and make sure to avoid integer division:


double ave;

ave = sum / 3.0;

Bandito
02-18-2014, 11:53 PM
// cap2 2.19.cpp : Defines the entry point for the console application.
//Este programa calcula la suma, resta, multiplicacion, promedio, el mas pequeno y el mas grande de tres numeros

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int n1, n2, n3, sum, sub, mult, ave, smallest, largest;//Inicializar variables
cout<<"Input three integers ";
cin>>n1>>n2>>n3;//Numeros que el usuario anade
sum = n1+n2+n3;//suma de los numeros
sub = n1+n2+n3;//resta de los numeros
mult = n1*n2*n3;//multiplicacion de los numeros
ave = sum/3;// este es los calculos del promedio
if (n1>n2)//Este if statement me calcula cual es el mas grande de los numeros
{
if(n1>n3)
largest = n1;
else
if (n2>n3)
largest = n2;
else
largest = n3;
}
else
{
if (n2>n3)
largest = n2;
else
largest = n3;
}
if (n1<n2)//este if statement me calcula el mas pequeno
{
if(n1<=n3)
smallest = n1;
else
if (n2<=n3)
smallest = n2;
else
smallest = n3;
}
else
{
if (n2<n3)
smallest = n2;
else
smallest = n3;
}
system("cls");
cout<<"Input three integers "<<n1<<" "<<n2<<" "<<n3<<endl;
cout<<"Sum is "<<sum<<endl<<"Average is "<<ave<<endl<<"Product is "<<mult<<endl<<"Smallest is "<<smallest<<endl<<"Largest is "<<largest<<endl;
system("pause");
return 0;
}



Got it, it now works.

Bandito
02-18-2014, 11:54 PM
Also, your average variable should be a float or double and make sure to avoid integer division:


double ave;

ave = sum / 3.0;
I want it to be an integer :lol

I don't want decimals on that shiet.:roll:

Bandito
02-18-2014, 11:55 PM
Came to say hello
sup:cheers:



:roll:

shaq2000
02-19-2014, 12:01 AM
I want it to be an integer :lol

I don't want decimals on that shiet.:roll:

Well, then you won't always be getting the average. :D

ace23
02-19-2014, 12:04 AM
I want it to be an integer :lol

I don't want decimals on that shiet.:roll:
Do you not want to round up in any case?

Bandito
02-19-2014, 12:08 AM
Well, then you won't always be getting the average. :D
True, but I am going by the book for now. It will probably start to talk about float, double, char etc...in the next chapter... I am going by the book so I can understand the basics first.

Bosnian Sajo
02-19-2014, 12:09 AM
Kobe > Lebron

Bandito
02-19-2014, 12:10 AM
Do you not want to round up in any case?
nope:D

Bandito
02-19-2014, 12:10 AM
Kobe > Lebron
I am going to make an app like that and call it ISH app or something just for the lulz....:roll:

cuad
02-19-2014, 12:16 AM
Writing a line like this
int largest; is called "declaring an int". This is not the same as "initializing an int". Initializing an int looks like
int largest; // declare
largest = 0; // initialize

or

int largest = 0; // declare and initialize on the same line. Some languages/compilers will automatically initialize ints to 0, but I guess C++ or your compiler doesn't.

Bandito
02-19-2014, 12:20 AM
Writing a line like this is called "declaring an int". This is not the same as "initializing an int". Initializing an int looks like Some languages/compilers will automatically initialize ints to 0, but I guess C++ or your compiler doesn't.
Oh in C++ when they mean by initialize they mean that the variable doesn't have an value assigned to it after it was declared. At least that's how I took it when I saw the error in the compiler.

But yes you are right.

MadeFromDust
02-19-2014, 12:23 AM
Make it pop-up a window and output to that instead of using "console" and ill-advised "system" calls :(

nathanjizzle
02-19-2014, 01:09 AM
Any one know where I can start learning python

Bandito
02-19-2014, 07:25 AM
Any one know where I can start learning python
If you already know how to program just check out any good phyton website. I bought a book called phyton : programming from oreilly which i am using to learn phyton.

embersyc
02-19-2014, 08:15 AM
Unless you specifically need the windows forms, check out Qt.

http://qt-project.org/

It's free and open source and really optimized for mobile development.

Who knew there were so many programmers on ISH?

:cheers:

sundizz
02-19-2014, 03:25 PM
what the hell is being posted in here. People do this stuff still...we don't have robots for this sort of nonsense yet :biggums:

ace23
02-19-2014, 03:47 PM
If you already know how to program just check out any good phyton website. I bought a book called phyton : programming from oreilly which i am using to learn phyton.
How did you misspell Python three times?

sixerfan82
02-19-2014, 09:13 PM
what the hell is being posted in here. People do this stuff still...we don't have robots for this sort of nonsense yet :biggums:

obvious troll is obvious