Sponsored Content
Homework and Emergencies Homework & Coursework Questions Problem with simple octal to decimal code Post 302511028 by qf_woodfox on Tuesday 5th of April 2011 02:34:41 PM
Old 04-05-2011
Problem with simple octal to decimal code

Hello all
I started computer science and C++ programming only 6 weeks ago with no prior knowledge. It's a great language, the only other one apart from English that I hope to speak fluently quite soon. I look forward to participating further on these forums, seeking answers and looking at previous threads. Hopefully I will be able to help others later on. Let me cut to the chase.

1. The problem statement, all variables and given/known data:
Although this is an assignment question, it isn't due for a while, and I'm mainly only awake at 4:20am for the sake of it. The question asks to write a C++ program that converts a 4-digit octal number to decimal. I wrote and compiled it, but there's something wrong. I don't think it's arithmetic, because the program terminates at return 1, even if there isn't an 8 or a 9.


2. Relevant commands, code, scripts, algorithms:
Code:
#include <iostream>
using namespace std;
int main()
{
    int number = 0;
    int digit4 = 0;
    int digit3 = 0;
    int digit2 = 0;
    int digit1 = 0;
    int decimal = 0;

    cout << "Enter a four digit octal number to convert to decimal form: " << endl;
        cin >> number ;

    ( digit4 = number/1000 );
    ( digit3 = number/100 - digit4*1000 );
    ( digit2 = number/10 - digit4*1000 - digit3*100 );
    ( digit1 = number - digit4*100 - digit3*100 - digit2*10);

    cout << digit1 << digit2 << digit3 << digit4 << endl; // This was me testing each digit. 

    if (digit1 || digit2 || digit3 || digit4 == 8 || 9)
    {
        cout << "The number entered contains the digit 8 and/or 9. Please try again." << endl;
        return 1;
    } 
    if (number > 7777)
    {
        cout << "The number entered exceeds the limit for this program, please try again." << endl;
        return 2;
    }
    (decimal = digit1 + digit2*8 + digit3*64 + digit4*512);

    cout << "The entered octal number is equal to: " << decimal << "in decimal form." << endl;

return 0;
}

3. The attempts at a solution (include all code and scripts):
As you can see in the code, i checked the output of each digit. It returned a dud result.

Tried removing the if statement testing for 8s and 9s. Returned a large negative number.

Also tried other fine-tuning methods to no avail.


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University of Wollongong, Wollongong, Australia, Daniel Saffioti, CSCI114
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash]printf octal instead of decimal

Hello everybody, I would like to understand why the printf function is returning me an octal value with this command : printf %4.4d 0010 returns 0008 printf %4.4d 10 returns 0010 Thanks for help. (3 Replies)
Discussion started by: dolphin06
3 Replies

2. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

3. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

4. Shell Programming and Scripting

Unknown (probably) simple problem with my code

I'm relatively new to both UNIX and Linux and slightly less new at programming. But I can't figure out why this won't run properly. It's meant to calculate the GCD of two numbers (simple enough, you'd think). And I designed it myself and it looks good to me and my instructor won't respond. If... (1 Reply)
Discussion started by: Sevchenko
1 Replies

5. Shell Programming and Scripting

How to convert a file containing hex code to decimal using script?

The file contains code like the below and need to convert each one into a decimal 00 00 00 04 17 03 06 01 So the output should come as 0 0 0 4 23 3 6 1 (24 Replies)
Discussion started by: necro98
24 Replies

6. Shell Programming and Scripting

Decimal number calculation problem

I have a below snippet of code from my perl script and its causing a problem when the output of $lTAX is 0 (zero) its getting displayed as -0.00. I want output to be 0 not -0.00. Any help would be appreciated. #!/usr/bin/perl my $lTotA = 50.94; my $lVatA = 8.49; my $lAllocD; my $lAdjNr =... (4 Replies)
Discussion started by: Devesh5683
4 Replies

7. Shell Programming and Scripting

Facing problem while printing accurate decimal value

Hi, I have a file tp.txt having below data CE2DD,N,5055,16.12.2013,3.114,12195.89,MVR,003388,Web::00000005055,Web Payment and i am using below code to print the values for var_amt_pay in `awk -F',' '{ arr += $6} END {for (i in arr) {print i "," arr } }' tp.txt` do ... (2 Replies)
Discussion started by: ranabhavish
2 Replies

8. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

9. Shell Programming and Scripting

Problem with gstat and octal value

Here are a few lines from my script. The problem I am having is that the statement for gstat returns this error: line 43: The statement is coming from gstat. Is there a way to fix it? Apparently -eq 02 is coming up as some octal value, I need it to be recognized as 02. Apparently in... (4 Replies)
Discussion started by: newbie2010
4 Replies
SPROF(1)							 Linux User Manual							  SPROF(1)

NAME
sprof - read and display shared object profiling data SYNOPSIS
sprof [option]... shared-object-path [profile-data-path] DESCRIPTION
The sprof command displays a profiling summary for the shared object (shared library) specified as its first command-line argument. The profiling summary is created using previously generated profiling data in the (optional) second command-line argument. If the profiling data pathname is omitted, then sprof will attempt to deduce it using the soname of the shared object, looking for a file with the name <soname>.profile in the current directory. OPTIONS
The following command-line options specify the profile output to be produced: -c, --call-pairs Print a list of pairs of call paths for the interfaces exported by the shared object, along with the number of times each path is used. -p, --flat-profile Generate a flat profile of all of the functions in the monitored object, with counts and ticks. -q, --graph Generate a call graph. If none of the above options is specified, then the default behavior is to display a flat profile and a call graph. The following additional command-line options are available: -?, --help Display a summary of command-line options and arguments and exit. --usage Display a short usage message and exit. -V, --version Display the program version and exit. CONFORMING TO
The sprof command is a GNU extension, not present in POSIX.1. EXAMPLE
The following example demonstrates the use of sprof. The example consists of a main program that calls two functions in a shared object. First, the code of the main program: $ cat prog.c #include <stdlib.h> void x1(void); void x2(void); int main(int argc, char *argv[]) { x1(); x2(); exit(EXIT_SUCCESS); } The functions x1() and x2() are defined in the following source file that is used to construct the shared object: $ cat libdemo.c #include <unistd.h> void consumeCpu1(int lim) { int j; for (j = 0; j < lim; j++) getppid(); } void x1(void) { int j; for (j = 0; j < 100; j++) consumeCpu1(200000); } void consumeCpu2(int lim) { int j; for (j = 0; j < lim; j++) getppid(); } void x2(void) { int j; for (j = 0; j < 1000; j++) consumeCpu2(10000); } Now we construct the shared object with the real name libdemo.so.1.0.1, and the soname libdemo.so.1: $ cc -g -fPIC -shared -Wl,-soname,libdemo.so.1 -o libdemo.so.1.0.1 libdemo.c Then we construct symbolic links for the library soname and the library linker name: $ ln -sf libdemo.so.1.0.1 libdemo.so.1 $ ln -sf libdemo.so.1 libdemo.so Next, we compile the main program, linking it against the shared object, and then list the dynamic dependencies of the program: $ cc -g -o prog prog.c -L. -ldemo $ ldd prog linux-vdso.so.1 => (0x00007fff86d66000) libdemo.so.1 => not found libc.so.6 => /lib64/libc.so.6(0x00007fd4dc138000) /lib64/ld-linux-x86-64.so.2(0x00007fd4dc51f000) In order to get profiling information for the shared object, we define the environment variable LD_PROFILE with the soname of the library: $ export LD_PROFILE=libdemo.so.1 We then define the environment variable LD_PROFILE_OUTPUT with the pathname of the directory where profile output should be written, and create that directory if it does not exist already: $ export LD_PROFILE_OUTPUT=$(pwd)/prof_data $ mkdir -p $LD_PROFILE_OUTPUT LD_PROFILE causes profiling output to be appended to the output file if it already exists, so we ensure that there is no preexisting pro- filing data: $ rm -f $LD_PROFILE_OUTPUT/$LD_PROFILE.profile We then run the program to produce the profiling output, which is written to a file in the directory specified in LD_PROFILE_OUTPUT: $ LD_LIBRARY_PATH=. ./prog $ ls prof_data libdemo.so.1.profile We then use the sprof -p option to generate a flat profile with counts and ticks: $ sprof -p libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls us/call us/call name 60.00 0.06 0.06 100 600.00 consumeCpu1 40.00 0.10 0.04 1000 40.00 consumeCpu2 0.00 0.10 0.00 1 0.00 x1 0.00 0.10 0.00 1 0.00 x2 The sprof -q option generates a call graph: $ sprof -q libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile index % time self children called name 0.00 0.00 100/100 x1 [1] [0] 100.0 0.00 0.00 100 consumeCpu1 [0] ----------------------------------------------- 0.00 0.00 1/1 <UNKNOWN> [1] 0.0 0.00 0.00 1 x1 [1] 0.00 0.00 100/100 consumeCpu1 [0] ----------------------------------------------- 0.00 0.00 1000/1000 x2 [3] [2] 0.0 0.00 0.00 1000 consumeCpu2 [2] ----------------------------------------------- 0.00 0.00 1/1 <UNKNOWN> [3] 0.0 0.00 0.00 1 x2 [3] 0.00 0.00 1000/1000 consumeCpu2 [2] ----------------------------------------------- Above and below, the "<UNKNOWN>" strings represent identifiers that are outside of the profiled object (in this example, these are instances of main()). The sprof -c option generates a list of call pairs and the number of their occurrences: $ sprof -c libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile <UNKNOWN> x1 1 x1 consumeCpu1 100 <UNKNOWN> x2 1 x2 consumeCpu2 1000 SEE ALSO
gprof(1), ldd(1), ld.so(8) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2017-09-15 SPROF(1)
All times are GMT -4. The time now is 04:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy