473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Case bypasses initialization of a local variable

1
#include <iostream.h>
#include <conio.h>
int main()
{
int a;
cout<< "ingrese un numero entre 1 y 4 gracias\n";
cin>> a;
switch(a)
{
case 1:
cout<< "este es el comienzo de c++ \n";
break;

case 2:
cout<< "Tabla de Multiplicar\n";
int num=0;
int res=0;

cout<< "ingrese un numero\n";
cin>> num;

for (int i=1; i<=10; i++)
{
res=num*i;
cout<< num<< "*" <<i<< "=" <<res<< "\n";
}
break;

case 3:
for(int e=1; e<=1000; e++)
{
cout<<"Ya Casi Terminas El Programa \n";
}
break;

case 4:
int n=1;
cout<< "Ingrese La Nota De Un Alumno\n";
if(n>=60 && n<=100)
{
cout<< "Gano El Curso\n";
}
else
{
if(n>=0 && n<=59)
{
cout<< "Perdio El Curso\n";
}
else
{
cout<< "Nota No Valida\n";
}
break;
}
}
getch ();
return 0;
}
Apr 16 '14 #1
1 2130
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. cin >> a
does not guarantee that the variable is initialized. It will be initialized only if the cin stream is not in error.

Just set the variable to 0 when you define it.
Apr 16 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
6
by: romerun | last post by:
Hello, This make me very confusing: ---------------------------------- int &kkk() { int a = 5; return a; }
3
by: John Smith | last post by:
In the following (pseudo)code, why is it that C# returns an "unassigned local variable" error for nVar? It seems I have to declare the variable outside foo() for the error to dissapear. void...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
2
by: silverburgh.meryl | last post by:
Can you please tell me what is the meaning this error in general? UnboundLocalError: local variable 'colorIndex' referenced before assignment In my python script, I have a variable define and...
7
by: gyan | last post by:
follwing code gives error: 1 #include<iostream.h> 2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 }
5
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were...
2
by: Jess | last post by:
Hello, I understand that if a function "f" has a local variable "a", and after it returns, "a" vanishes. If "f" returns "a" as a result, then I noticed the following: 1. if the return type is...
1
by: xobserver | last post by:
hi my frnds i wish someone can help me with this error i have highlighted the errors in block letters i want urgent help plz .. i must give project tomorrow #include<fstream.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.