12 Replies - 1169 Views - Last Post: 04 July 2012 - 06:23 AM Rate Topic: -----

#1 shena12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 12

How to make my global variables into local variables ?

Posted 03 July 2012 - 07:12 AM

i have a project about enqueue and dequeue...my program is fairly good in running, but our instructor told us not to use global variables..the problem is that i have..now, when i try to make my variables into local,it makes my program a mess..so any suggestions, remarks and ideas are greatly appreciated...thanks by the way!

here is my code:

# include <iostream>
using namespace std;

void en();

void de();

void display(int[]);

void menu(int);

void end();

int arrayNum[6], num=-1;

int main()
{
	int choose;
		
	cout<<"\n\n\tMENU"<<endl;
	cout<<"   [1] - Enqueue"<<endl;
	cout<<"   [2] - Dequeue"<<endl;
	cout<<"   [3] - Exit"<<endl;
	
	cout<<"Decision: ";
	cin>>choose;
	cout<<endl<<endl;


	menu(choose);

	return 0;

}

void menu(int choose)
{
	
	system("cls");
	

		switch(choose)
	{
	case 1:
		{
			
			en();


		}
		break;
	case 2:
		{
			de();
		}
		break;
	
	case 3:
		{
			system("pause");

			cout<<endl;

			system("cls");

			cout<<"\n \n \n \t \tYou are now ending the program..."<<endl<<endl<<endl;

			exit(1);
		}
		break;
	default:
		{
			cout<<"Decision is not on the options. :[ "<<endl;
		}
	
	}
	end();
	
}

void en()
{
	
		int  item;			 
	
		cout<<"\n\tEnter a number to enqueue: ";
		cin>>item;
		cout<<endl;

		if(num==5)
			cout<<"\n\n\t Array is full :["<<endl;
		else
		{
			num= num + 1;
			arrayNum[num]= item;
			
		}

		display(arrayNum);
			

}

void de()
{
	int start=0;

	if(num==-1)  
  
        {  
  
            cout<<"\n\n\n\t\t No elements in the Array left :[\n"; 
			
			  
        }  
  
        else 
		{
			for(int p=0; p<6; p++)
				arrayNum[p]=arrayNum[p+1];
			    arrayNum[6]= 0;
				num--;

		}

		display(arrayNum);

		

}

void display(int arrayNum[])
{

	cout<<endl<<endl<<"\tThese are the current elements in the array:"<<endl;

	
	for(int x=0; x<6; x++){
		cout<<"   [ "<<arrayNum[x]<<" ]   ";
	}

	cout<<endl<<endl;

	end();

}



void end()
{
	char ch;

	cout<<endl<<endl;

	cout<<"\t \tDo you want to exit the program?(y/n): ";
	cin>>ch;
	cout<<endl;

	if (ch == 'y' || ch == 'Y'){
		cout<<endl<<endl;
		
		system("pause");

		cout<<endl<<endl;

		exit(1);
	}

	if (ch == 'n' || ch == 'N'){
		cout<<endl<<endl;

		system("pause");

		system("cls");

		main();
	}

}
	


This post has been edited by modi123_1: 03 July 2012 - 07:18 AM
Reason for edit:: fixed silly sizing


Is This A Good Question/Topic? 0
  • +

Replies To: How to make my global variables into local variables ?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,883
  • Joined: 25-December 09

Re: How to make my global variables into local variables ?

Posted 03 July 2012 - 07:26 AM

Quote

but our instructor told us not to use global variables

Good, using global variables is a very bad practice.

Quote

.the problem is that i have..now, when i try to make my variables into local,it makes my program a mess

How does using local variables make your program a mess? Show an example of what you think is a mess.

You will need to properly pass the required variables to and from your functions using parameters and proper function return values.

In C++ programs you should also avoid using the exit() function whenever possible. This C function doesn't always properly call C++ class destructors properly. And you should also avoid using the system() function as well.


Jim

This post has been edited by jimblumberg: 03 July 2012 - 07:29 AM

Was This Post Helpful? 1
  • +
  • -

#3 shena12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 12

Re: How to make my global variables into local variables ?

Posted 03 July 2012 - 07:44 AM

okay..i will always remember that..thank you..i have deleted my trial for making my global variables into local...i will update you on my codes after i try it again and i will post it again..thanks
Was This Post Helpful? 0
  • +
  • -

#4 redhat69  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 28-June 12

Re: How to make my global variables into local variables ?

Posted 03 July 2012 - 07:44 AM

You are going to need to create parameters for your functions in order to access the local vaiables you are creating. You will also need to return the vars and values. :w00t:
Was This Post Helpful? 0
  • +
  • -

#5 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: How to make my global variables into local variables ?

Posted 03 July 2012 - 08:28 AM

An alternative is to continue using void functions, but to utilize pass-by-reference and alter the variables & values inside the functions. This is good if your professor wants you to use void functions.

The default in C++ is to pass-by-value which creates a copy of the variable when it is passed into a function as an argument. When you pass-by-value, the parameters can be manipulated inside your function without altering the original arguments that were passed in.

However, if you were to pass-by-reference, any changes you make to the parameters in the function will also change the original arguments that you passed in.

//example
#include<iostream>

void passByValue(int val) {

    val = 20; //will not change v
}

void passByReference(int &ref) {

    ref = 20; //will change r
}

int main() {

    int v, r;
    v = 10;
    r = 10;
    passByValue(v);
    passByReference(r);
    std::cout << "v = " << v << std::endl; //v is unchanged
    std::cout << "r = " << r << std::endl; //r has been altered
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: How to make my global variables into local variables ?

Posted 03 July 2012 - 08:29 AM

You're using magic numbers all over the place! Stop that. Why 6? Why not 42? Yes, you know that's the size, but how does the program know? Pass the size, use the size.

And, do not call main! Ever, ever, ever, bad. Why not pass back a value?

e.g
#include <iostream>

using namespace std;

void enqueue(int[] array, int size, int &num);
void enqueue(int[] array, int size, int &num);
void display(int[] array, int size);
int showMenu();
bool processMenu(int, int[] array, int size, int &num);
bool end();


int main() {
	const int size = 6;
	int arrayNum[size], num = -1;

	while(processMenu(showMenu(), arrayNum, size, num));

	return 0;

}

int showMenu() {
	int choose;
		
	cout<<"\n\n\tMENU"<<endl;
	cout<<"   [1] - Enqueue"<<endl;
	cout<<"   [2] - Dequeue"<<endl;
	cout<<"   [3] - Exit"<<endl;
	
	cout<<"Decision: ";
	cin>>choose;
	cout<<endl<<endl;
	return choose;
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1706
  • View blog
  • Posts: 3,273
  • Joined: 30-May 10

Re: How to make my global variables into local variables ?

Posted 03 July 2012 - 09:07 AM

> .the problem is that i have..now, when i try to make my variables into local,it makes my program a mess
That's because you had an array overrun in your code.

014 int arrayNum[6], num=-1;
...
121 arrayNum[6]= 0;

With a global, you got "lucky", trashed something which didn't matter (in this case), and you perceived "success" and "bug free".

With a local, you got "unlucky", trashed something important and the error was exposed.

Never ever believe that the first time something seems to work, that it is also magically bug-free.

This post has been edited by Salem_c: 03 July 2012 - 09:36 AM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#8 shena12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 12

Re: How to make my global variables into local variables ?

Posted 04 July 2012 - 05:49 AM


thanks for the ideas and suggestions guys..i have done the best of what i think is...let me explain why i chose array_size to 6 because that's what the instructor told us to..and i have passed arrays...but i still have errors: when displaying arrays, the other empty arrays, outputs garbage elements...i think I've got the passing of arrays a little bit... here, my updated codes:

# include <iostream>
using namespace std;

void de(int[],int);

void display(int[]);

int main()
{
	
	int arrayNum[5], num=0, item, val=0;
	int choose;
	char ch;
	
	do{

	cout<<"\n\n\tMENU"<<endl;
	cout<<"   [1] - Enqueue"<<endl;
	cout<<"   [2] - Dequeue"<<endl;
	cout<<"Decision: ";
	cin>>choose;
	cout<<endl<<endl;

	
	system("cls");
	

		switch(choose)
	{
	case 1:
		{
			
			cout<<"\n\tEnter a number to enqueue: ";
			cin>>item;
			cout<<endl;

			if(num==5)
			cout<<"\n\n\t Array is full :["<<endl;
			else
			{
				
				arrayNum[num]= item;
				num= num + 1;
			
				val=num;
			display(arrayNum);
			}

		}
		break;
	case 2:
		{
			de(arrayNum, val);
		}
		break;
	
	default:
		{
			cout<<"Decision is not on the options. :[ "<<endl;
		}
	
	}
	


	cout<<"Do you want to exit?:";
	cin>>ch;
	cout<<endl<<endl;

	}while(ch=='n'|| ch=='N');

	cout<<"Thank you..."<<endl<<endl<<endl;

	return 0;

}



void de(int arrayNum[], int num)
{
	if(num==-1)  
  
        {  
  
            cout<<"\n\n\n\t\t No elements in the Array left :[\n"; 
			
			  
        }  
  
        else 
		{
			for(int p=0; p<5; p++)
				arrayNum[p]=arrayNum[p+1];
			    arrayNum[5]= 0;
				num--;

		}

		display(arrayNum);

		

}

void display(int arrayNum[])
{

	cout<<endl<<endl<<"\tThese are the current elements in the array:"<<endl;

	
	for(int x=0; x<6; x++){
		cout<<"   [ "<<arrayNum[x]<<" ]   ";
	}

	cout<<endl<<endl;

	
}






Was This Post Helpful? 0
  • +
  • -

#9 shena12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 12

Re: How to make my global variables into local variables ?

Posted 04 July 2012 - 05:56 AM


i think i figured it out..my display function should pass the limit number..

void display(int arrayNum[], int num)
{

	cout<<endl<<endl<<"\tThese are the current elements in the array:"<<endl;

	
	for(int x=0; x<num; x++){
		cout<<"   [ "<<arrayNum[x]<<" ]   ";
	}

	cout<<endl<<endl;

	
}




but i have encountered another error..when i enqueue two numbers, then dequeue..then enqueue again...the display will show a garbage element in between the late number and the latest input number...

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,883
  • Joined: 25-December 09

Re: How to make my global variables into local variables ?

Posted 04 July 2012 - 06:10 AM

Quote

when displaying arrays, the other empty arrays, outputs garbage element

When you display your queue you need to only display the elements you have inserted into the queue. The other values are uninitialized so they will contain garbage.

Have you learned about structures yet?

Jim
Was This Post Helpful? 0
  • +
  • -

#11 shena12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 12

Re: How to make my global variables into local variables ?

Posted 04 July 2012 - 06:12 AM

ahh...i see...we haven't touch the topic structures...
Was This Post Helpful? 0
  • +
  • -

#12 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1706
  • View blog
  • Posts: 3,273
  • Joined: 30-May 10

Re: How to make my global variables into local variables ?

Posted 04 July 2012 - 06:14 AM

052	        {
053	            de(arrayNum, val);
054	        }


Decide whether it is 'num' or 'val' which keeps track of the number of elements in your array.


Then try this code
        {
            cout << "Before=" << val << endl;
            de(arrayNum, val);
            cout << "After=" << val << endl;
        }


What do you see here?
What does the num-- INSIDE the function achieve?

This post has been edited by Salem_c: 04 July 2012 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

#13 shena12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 12

Re: How to make my global variables into local variables ?

Posted 04 July 2012 - 06:23 AM

alright...
num--; decreases the value of num..

oh a garbage line...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1