C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 8th, 2004, 03:32 AM
program_me program_me is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 1 program_me User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
having problem with the output

This is program i refer from the book c++ for dummies 5 edition
by Stephen Randy davis, since i am just a beginner i try to type
and compile it, it runs fine, but the problem is i could not get the
correct output. The output for nArgs1 is suppose to be 0x1234,
but what the output display in my screen was 0x4660 which is
the decimal of hexadecimal 0x1234, can anyone help me with this?
Your help is much appreciated.Thank you.

//BitTest- initialize two variables and output the
//results of applying the ~, &, | and ^
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int main(int nNumberofArgs, char * pszArgs[])
{
//set output format to hexadecimal

cout.setf(cout.hex);

//initialize two arguments
int nArg1;
nArg1=0x1234;

int nArg2;
nArg2=0x00ff;

//now perform each operation in turn
//first the unary NOT operator;


cout<<"Arg1=0x"<<nArg1<<"\n";
cout<<"Arg2=0x"<<nArg2<<"\n";
cout<<"~nArg1=0x"<<~nArg1<<"\n";
cout<<"~nArg2=0x"<<~nArg2<<"\n";

//now the binary operators
cout<<"nArg1 & nArg2=0x"<<(nArg1 & nArg2)<<"\n";
cout<<"nArg1 | nArg2=0x"<<(nArg1 | nArg2)<<"\n";
cout<<"nArg1 ^ nArg2=0x"<<(nArg1 ^ nArg2)<<"\n";

//wait until user is ready before terminating program
//to allow the user to see the program results

system("PAUSE");
return 0;
}

Reply With Quote
  #2  
Old August 4th, 2006, 12:01 AM
tyggna tyggna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 1 tyggna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 43 sec
Reputation Power: 0
Also a dummy

I ran into the problem too with the "for Dummmies" example program. I believe it's just a minor syntax problem. I have two fixes that worked for me.

The first regards this line:
cout.setf(cout.hex);

This line is supposed to change the output of the program into hexidecimal, but failed to do so for me. So, the alternative is:
cout.setf(ios::hex, ios::basefield);

now for my guess as to why this works and the other doesn't, if someone who knows better knows better, than please correct my erronious thinking.

ios means that you're referring to the iostream file that you #include at the beginning. The iostream file contains the nessisary code to display something in hexidecimal, but for some reason or another can't figure out that the (cout.hex) means that you want all variables to be displayed in hex.
I think that it doesn't realize that you want to CHANGE from the default output (which is decimal). Since 4660 (base 10) == 1234 (base 16). Therefore, the solution is to call the basefield aspect of the iostream to it's attention as a parameter for the cout command.

The other fix is equally simple to implement, and even simpler to explain.

on the four lines that read as follows:
cout << "Arg1 = 0x" << nArg1 << "\n";
cout << "Arg2 = 0x" << nArg2 << "\n";
cout << "~nArg1 = 0x" << ~nArg1 << "\n";
cout << "~Arg2 = 0x" << ~nArg2 << "\n";

simply tell the first line that you want it to read in hexidecimal, and the rest should follow suit. Like so:
cout << hex << "Arg1 = 0x" << nArg1 << "\n";
cout << "Arg2 = 0x" << nArg2 << "\n";
cout << "~nArg1 = 0x" << ~nArg1 << "\n";
cout << "~Arg2 = 0x" << ~nArg2 << "\n";

Man, I sure learned a lot about output today (I started the for dummies book yesterday, no C++ experience. If anyone else can provide insight or additional fixes, I'd appreciate it)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > having problem with the output


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek