C/C++ :: Local Variable Declaration - While Loop Expression

Feb 3, 2013

I got the following lines of code from a book. The variable char c is being used here to demonstration local variable declaration.

  while(char c = cin.get() != 'q') {
    cout << c << " wasn't it" << endl;
    if(char x = c == 'a' || c == 'b')
      cout << "You typed a or b" << endl;
    else
      cout << "You typed " << x << endl;
  }

It compiles and runs. There are two issues when I try to run it.

1) It seems to loop through two times for every entry. If I insert cin.ignore() just before the closing bracket, it seems to work better.

2) the variable c does not seem to have the character I entered when examined in the if statement.

What is happening with the variable c inside the while loop scope?

Does c actually get initialized?

View 2 Replies


ADVERTISEMENT

C :: String Declaration As Global / Main Local Variable

Nov 14, 2013

When a declare a string e.g.

Code:
char str[30]; as a global variable, the srting is initialized to NULL.

But if I declare char str1[30] INSIDE main(), then the string has garbage inside.... Why this happens??

E.g. the following code snippet...

Code:
#include <stdio.h>
char str[50];
int main(){
char str1[50];

[Code] ....

View 4 Replies View Related

C++ :: Difference Between Variable Declaration Inside And Outside For Loop

Sep 10, 2013

What is difference (memory allocation or any) between declaring a variable inside or outside the variable

program1:

#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {

[Code] .....

Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.

View 1 Replies View Related

C :: Transform Local Variable Into Global Variable?

Oct 25, 2014

I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:

Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);

So, I need to transform the final value of "count" into a global variable. Can I do this?

View 5 Replies View Related

C/C++ :: Function With Local Variable

Jul 28, 2014

#include<iostream>
#include<stdlib.h>
using namespace std;
int Name(),Minimum(),Maximum();
int main() {
int name=Name(),minimum= Minimum(),maximum= Maximum();

[Code] ...

There are error to let user to key in minimum and maximum values, i would like to know whats the problem?

View 1 Replies View Related

C :: Returning A Pointer To Local Variable

Aug 18, 2014

Should i never return a pointer to a local variable. i have seen the following code in the standard text book.

Code:

struct node *add_to_list(struct node *list, int n)
{
struct node *new_node;
new_node = malloc(sizeof(struct node));
// some code here
return new_node;
}

new_node is a local pointer then why is he returning the new_node? Is it wrong or am i failing to understand something?

View 2 Replies View Related

C++ :: Temp Or Local Variable Warning

Feb 28, 2014

Here is my overloaded operator :

const double & Waz::operator()(int i,int j) const {
return ((WazImpl const)(*p))(i,j);
}

Where in Waz class I have : WazImpl* p; and in the class WazImpl I have an operator () const

The warning is : Warning C4172 returning address of local variable or temporary

As far as I understand I'm returning a temp variable which is destroyed somewhere else what can I do to fix it?

View 2 Replies View Related

C++ :: Returning Address Of A Local Variable?

Jul 9, 2014

I know that this code is wrong because it returns the address of local variable which no longer exist after function:

int *f1()
{
int x = 2;
return &x;
}

However, why this one does not show problem and runs fine, here the pointer p is also a local variable:

int *f2()
{
int x = 2;
int *p;
p = &x;
return p;
}

View 6 Replies View Related

C# :: Unassigned Local Variable Error?

Jan 27, 2015

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Furniture {
class Program {
static void Main(string[] args)

[Code] ....

I tried changing the type of variable to char but i still get the same result. i also tried using a switch statement which was my first choice but i had the same issue.

View 2 Replies View Related

C/C++ :: Uninitialized Local Variable Errors

Sep 7, 2014

I keep getting the "Uninitialized Local Variable" error. But for my code it's says it's the variable 'pay' in my Manager Function. This is the only error that is popping up.

I've tried setting pay to 0 but when I do, I get a bunch of external errors. I've also tried assigning pay to WeeklySalary like this:

double pay = WeeklySalary;

//Calculating pay for a company

#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
double managerFunction();
double hourlyWorkerFunction();
double commissionWorkerFunction();

[Code] .....

View 10 Replies View Related

C# :: Unassigned Local Variable In Pay Calculator

Nov 15, 2014

{
//Declare Variables
decimal hrsWrkd, otPay, grossPay, taxRate=0, taxAmount, netPay,basePay, wage;
string maritalStatus;
const decimal WORKWEEK = 40;
const decimal OTRATE = 1.5m;
const double m = 0.15;

[Code] ....

The error message I am getting is Use of unassigned local variable 'otPay' . I see its been declared and been used in the code so the error is confusing, also when i run the program i noticed that it doesn't take out the taxes.

View 4 Replies View Related

C :: Return Local Variable - Infix To Postfix

Nov 21, 2014

So I have been working my way through this assignment and I'm just stuck now. I cannot get this work properly It just tells me I'm trying to return a local variable when attempting to return postfix in the to_postfix function. It is line 97 that wont compile. Also I cannot change anything in runner.c.

Calculator.c
Code:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include "stack.h"

[Code] .....

View 11 Replies View Related

C/C++ :: Case Bypasses Initialization Of Local Variable

Apr 16, 2014

#include <iostream.h>
#include <conio.h>
int main() {
int a;
cout<< "ingrese un numero entre 1 y 4 gracias
";
cin>> a;
switch(a)

[code].....

View 1 Replies View Related

C/C++ :: Create Local Instances Of A Global Variable?

Oct 4, 2012

is it possible to have a global variable pointing to a different address depending on the thread?

Imagine one would like to use threads with the loop:

for (i=0;i<n;i++){
globalPointerVariable=getAddress(i);
DoThingsUsingThe_globalPointerVariable();
}

View 4 Replies View Related

C++ :: How To Pass Address Without Creating Local Variable

May 14, 2012

I am doing a piece of gui code on some embedded system.

I'm trying to find a way of eliminating the local variable kEvent:

Code:
EVENT kEvent;
....

Code:
kEvent = EVENT_UPSTREAM;
xStatus = xQueueSendToBack(getEventProcessorQueue(),&kEvent, 0 );
....

I tried this, it doesn't work:

Code:
xStatus = xQueueSendToBack(getEventProcessorQueue(),(EVENT *)EVENT_UPSTREAM, 0 );

Shouldn't this work?

View 1 Replies View Related

C++ :: Difference Between Static Local Variable And Static Global Variable?

Aug 5, 2013

Here is the code,

Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}

So is there any difference between a defined in CreateObject and aa?

View 6 Replies View Related

C++ :: Get Error Case Bypasses Initialization Of A Local Variable

Apr 13, 2014

i always get error case bypasses initialization of a local variable

here's my coding

my header files

Code: #include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <fstream.h>

[Code].....

i'm using borland c++ v5.02

View 3 Replies View Related

C/C++ :: Error / Case Bypasses Initialization Of A Local Variable

Nov 22, 2013

i have highlighted the errors in block letters.

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void login();
struct date {
int dd,mm,yy;

[code].....

View 1 Replies View Related

Visual C++ :: Local Variable Be Passed As Parameter Into A New Thread?

Apr 1, 2013

Can local variable be passed as the parameter for a new created thread procedure? Here is the example code:

Code:
void CDLG::some_function()
{
CString strFileName="abc.doc";
//local variable, can it be valid for being passed into the following new thread???
//Can strFileName still be accessed from within the stack of thread procedure?
::AfxBeginThread(ProcessContentThread,(LPVOID)&strFileName);
}

[Code]...

There is another method using variable on the heap,

Code:

void CDLG::some_function()
{
CString strFileName="abc.doc";
CString* pstrFN=new CString(strFileName);
::AfxBeginThread(ProcessContentThread,(LPVOID)pstrFN);
}

[Code]...

I test these code, both methods work as expected, but I doubt whether the first method is a good way. OR if only the second method is the correct way to pass a parameter to a thread.

View 12 Replies View Related

C++ :: State Of Variable In One Expression

Mar 27, 2014

I thought that an operator performs a permanent change in a local variable. For example, if x is 00000000 00000000 00000000 00000011 (a 32 bit unsigned integer that resolves to value of 3) and p is 2, in the expression "return (x >> p) | (x << (s - p))", the right shift would permanently change x to 0 and the time we evaluate x again in "(x << (s - p))", x will already be 0. If that's the case, then this function doesn't make sense. This function makes it seem like that the >> and << operators do not change the value of x. It makes it seem like first we right shift x to 0 and then left shift by 20 bits to make x 11000000 00000000 00000000 00000000. If that's the case, then the function does exactly what it is supposed to do (rotate the bits). So which is it?

unsigned int rightrot(unsigned int x, unsigned int n) {
size_t s = sizeof(x) * CHAR_BIT;
size_t p;
if(n < s)
p = n;

[Code] ....

View 2 Replies View Related

C :: Declaration And Definition Of A Variable

May 11, 2013

I read that Memory is allocated during definition of a variable and not during declaration. Declaration is something like,

Code: int x;

And definition is assigning some value to it. This is what my professor taught. My doubt is if memory is not allocated during declaration, then how the compiler successfully compiles and runs the following, which i had already tried.

Code:
#include<stdio.h>
#include<conio.h>
int main() {
int c;
int *p=&c;
printf("%x",p);
getch();
return 0;
}

The variable c is only declared. But the program outputs a memory address. Shouldn't it show an error?

View 2 Replies View Related

C++ :: Static Variable Declaration

Oct 4, 2014

If i declare 2 variables like this static int first, second; will both of them be declared static or will only first be declared static and second a regular variable?

View 5 Replies View Related

C++ :: Class And Variable Declaration?

Aug 2, 2014

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class ir;
class Bank_acc {
private:
string name,type,s;
long int accno,temp,balance,in;

[Code]....

errors are:

|6|error: forward declaration of 'class ir'|
|54|error: invalid use of incomplete type 'class ir'|
|99|error: no matching function for call to 'ir::interest()'|

View 1 Replies View Related

Visual C++ :: Code Error C4700 - Uninitialized Local Variable Eligible Used

Mar 27, 2015

In Visual Studios I keep getting this error. cpp(36): error C4700: uninitialized local variable 'Eligible' used

Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
void Getinput(int& Loantype, double& Income, double& Totaldebt, double& Loanamount);

[Code] ....

View 4 Replies View Related

C :: Declaration And Definition Of External Variable

Jan 11, 2014

Why I take warning on this code :

Code:
#include<stdio.h>
extern int v = 0; // Declaration and definition of an external
int main(void)

[code]...

Is there any error? Why I take a warning: 'v' initialized and declared 'extern'|||=== Build finished: 0 errors, 1 warnings ===| ???

and what is the meaning of :An extern declaration that initializes a variable serves as a definition of the variable. This rule prevents multiple extern declarations from initializing a variable in different ways.

View 3 Replies View Related

C/C++ :: Expected Declaration Specifier In For Loop?

Mar 1, 2015

#include <stdio.h>
#include <math.h>
main() {
int n;
int k;
int j;
//gets user input for length of string
printf("Enter the value of n:");
scanf("%d", &n);

[code]......

When I compile my code I get an error saying that I need declaration specifiers in my is_prime subroutine in the for loop.

Here is the line of code it references.

for(j = 2; j < k; j++)

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved