GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2008, 01:47
bcompt143 bcompt143 is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 22
bcompt143 is on a distinguished road
Arrow

Critical Declaration question


CPP / C++ / C Code:
#include<iostream.h>
#Include<conio.h>

void main ( ) 
{
  int x;
  char y;
  cout<<"enter any number";
  cin>>x;
  cout<<"enter any character;
  cin>>y;
  cout<<x;
  cout<<y;
 getch();
}
In the above program if we enter integer in variable x it display, the number itself and if we enter character in y it display itself. but if we enter character in the place of x, it display the any number and if we enter integer in the place of y it display the number. my question is, why character accept and display the integer number. tell me the brief reason please.
  #2  
Old 11-Nov-2008, 02:29
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Critical Declaration quesiton


Quote:
Originally Posted by bcompt143
In the above program...
...which I assume you simply entered by hand without actually compiling. Errors include:
  • CPP / C++ / C Code:
    #Include<conio.h>
    ...is not a valid preprocessor command.
  • CPP / C++ / C Code:
      cout<<x;
      cout<<y;
    ...outputs the integer value of x followed by the character value of y on the same line with no separation.
  • ...& it is not nice to suppress the return value of main().
Your question is not entirely clear, but you should review how many bits comprise an int & char. On 32-bit systems, the following example:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main() {
    char c = 'a';
    int i = 'a';

    cout << c << "\t" << sizeof(c) << endl;
    cout << i << "\t" << sizeof(i) << endl;

    return 0;
}
...yields the following output:
Code:
a 1 97 4
...which points out that the char value of 'a' comprises one byte on a 32-bit system, & the int value (or ASCII value...) of 'a' is 97 consumes 4 bytes on a 32-bit system.

What happens when a 1-byte char value is assigned to a 4-byte int? Since ASCII values typically are 7-bits, the most significant bit is set to zero. When assigned to an int, the three most significant bytes of the int are also set to zero.
  #3  
Old 11-Nov-2008, 02:50
bcompt143 bcompt143 is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 22
bcompt143 is on a distinguished road

Re: Critical Declaration question


My question is not that, I want to ask, why varialbe declared as character accept integer and display the integer, and why not the variable declared as integer is does not display the character.
  #4  
Old 11-Nov-2008, 03:47
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Critical Declaration question


Quote:
character accept integer and display the integer,

As long as it is a character, it will display the character.

Integer is not allow to store character but you can display it ANSII value.
  #5  
Old 11-Nov-2008, 04:04
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 73
cpit is on a distinguished road

Re: Critical Declaration question


A variable of type character allows you to assign chars and numbers (like integer, float, etc...). However, you can´t use a variable of type char (even if it was assigned a number) to make calculations.

I hope this help you.
  #6  
Old 11-Nov-2008, 06:22
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Critical Declaration question


Quote:
Originally Posted by bcompt143
I want to ask, why varialbe declared as character accept integer and display the integer, and why not the variable declared as integer is does not display the character.
Present a case where this occurs. The code posted doesn't compile, nor have you provided a specific example which supports your question.

Actually, Peter_APIIT is right. Everything is stored as a numerical value. In the case of chars, the value is interpreted as an ASCII character, & if printable, the corresponding character glyph will be displayed. If the numeric value represents an unprintable ASCII character, nothing should be displayed.
  #7  
Old 11-Nov-2008, 09:45
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Critical Declaration question


you can assign a number to char because numbers also have an ascii values and they can be considered as just plain characters which does not have any other functionalities of numbers.

But you obviously cannot masquerade a charater as a number because a number is as a subset of chars with many special features which other chars dont have.

so this is expected to throw an error.
  #8  
Old 11-Nov-2008, 09:54
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Critical Declaration question


Quote:
Originally Posted by ocicat
Present a case where this occurs. The code posted doesn't compile, nor have you provided a specific example which supports your question.
.

eg: enter any alphabet when it ask for a number and it will break the normal execution. (Atleast if you are using vc++ 2008 )

run this again and enter a number when it asks for a number and enter a number again when it asks for a char. There is no problem in this case.

CPP / C++ / C Code:
#include <iostream>
//#include <exception>
using namespace std;

int main ( ) 
{
	int x;
	char y;
	cout<<"enter any number\n";
	//try{
	cin>>x;
	//}
	//catch (exception& e) {
	//	cout << e.what();
	//}
	cout<<"enter any character\n";
	cin>>y;
	cout<<"number = "<<x;
	cout<<"  char = "<<y;
	cout << endl;
	return 0;
}
  #9  
Old 11-Nov-2008, 11:18
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Critical Declaration question


you might want to use this code if you want to handle this issue.

CPP / C++ / C Code:
#include <iostream>
#include <limits>
using namespace std;

int main ( ) 
{
	int x;
	char y;
	while ((cout << "Enter any number ") && !(cin >> x)) {
		cout << "That's not a number; ";
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	cout<<"enter any character\n";
	cin>>y;
	cout<<"number = "<<x;
	cout<<"  char = "<<y;
	cout << endl;
	return 0;
}

(check this out: http://www.parashift.com/c++-faq-lit....html#faq-15.3)
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Error C2374 lolp1 C++ Forum 3 25-May-2008 17:40
Multiple questions for C++ project devster420 C++ Forum 1 20-Apr-2007 22:26
Implicit Declaration error mike3340 C++ Forum 1 16-Dec-2003 09:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:53.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.