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, 02: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 3rd, 2006, 11:01 PM
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!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

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 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek