473,513 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Case bypasses initialization of a local variable

1 New Member
#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 2142
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
2679
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
6070
by: romerun | last post by:
Hello, This make me very confusing: ---------------------------------- int &kkk() { int a = 5; return a; }
3
6461
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
3123
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
5013
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
25115
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
2237
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
2079
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
1532
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
7257
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
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
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
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
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...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.