5 Replies - 1030 Views - Last Post: 11 December 2012 - 01:22 PM Rate Topic: -----

#1 ry_ol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 12

Deleting int value

Posted 10 December 2012 - 08:03 PM

Hi, this is my first post so forgive me if I goof up.

I'm trying to write a sign in program and I need to get the value of the sign in code deleted so that it can be used again and recalculated later on. I've searched the internet for deleting int values and all it comes up with is deleting from point values or arrays and I don't have that. After the first run through it gets hung up on "Enter sign in code". I've also tried using a loop but couldn't get it to work...not sure if I did something wrong or not.

any help is appreciated thanks!

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

//Header files
#include "staff.h"



int main()
{	beginning:

	//Variable Declaration
	 int code=0;

	//Prompt for sign in code
	signIn:
	cout << "Enter sign in code: ";
	system("PAUSE");
	cin >> code;
	cout << endl;

		switch (code) 
		{
			case 0: 
				
				goto signIn;
				break;
			case 1:
				Me();
				goto beginning;
				break;
			case 2:
				You();
				break;
			default:
					cout << "value of x unknown";
		}
	
		

	//goto beginning;
//	system("PAUSE");

	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Deleting int value

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Deleting int value

Posted 10 December 2012 - 09:00 PM

Quote

I'm trying to write a sign in program and I need to get the value of the sign in code deleted so that it can be used again and recalculated later on.


Define what you mean by deleted? Remember that these variables are held in memory somewhere, the memory cannot be physically deleted; rather, variables can be "virtually" deleted by changing pointer locations, setting a flag, or changing the value to 0.

This post has been edited by jjl: 10 December 2012 - 09:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Deleting int value

Posted 10 December 2012 - 09:15 PM

Well, first of all, its not very good to use GOTOs in your program as it behaves very erroneously.
So, instead you could use something like a loop that asks the user what is the sign in code and keep him in the loop till he enters invalid code.

For the gotos to be replaced, you can write functions in the place of them and switch-cases for the selection. According to the user's requirements, you can call appropriate functions in your switch-case.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#4 ry_ol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 12

Re: Deleting int value

Posted 11 December 2012 - 12:29 PM

View Postjjl, on 10 December 2012 - 09:00 PM, said:

Quote

I'm trying to write a sign in program and I need to get the value of the sign in code deleted so that it can be used again and recalculated later on.


Define what you mean by deleted? Remember that these variables are held in memory somewhere, the memory cannot be physically deleted; rather, variables can be "virtually" deleted by changing pointer locations, setting a flag, or changing the value to 0.



by deleted I mean erasing the value of the variable from memory so that it can be used again, like starting the program over without starting it over. I know this is what loops can do but I haven't gotten while or for loops to work.
Was This Post Helpful? 0
  • +
  • -

#5 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 616
  • Joined: 11-June 11

Re: Deleting int value

Posted 11 December 2012 - 12:48 PM

Quote

I mean erasing the value of the variable from memory


Do you mean this:
int x = 4;
// perform some tasks on x

x = 0;   // overwrite x, erase the value of x


Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Deleting int value

Posted 11 December 2012 - 01:22 PM

View Postry_ol, on 11 December 2012 - 08:29 PM, said:

by deleted I mean erasing the value of the variable from memory so that it can be used again


There's no such thing as erasing a variable. Once a variable is initialized, it will always hold a value for as long as it lives. And a variable can always be used again - it does not have to be "erased" to do.

If you want to read input into the code variable a second time, all you have to do is to run cin >> code a second time. If that does not work, this must be due to factors else where in your code unrelated to you not "deleting" the code variable (for example, if there are other places in your code where you read from cin, those might have left it in an invalid state).

This post has been edited by sepp2k: 11 December 2012 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1