My program is to enter a decimal number and change it to any number with the declared base.. here are the parameters:
Thank you
Specific Requirements:
1. You must use the a STACK to store the individual digits.
2. You must output the converted base number as a sequence of digits.
3. Your program should continue to prompt the user to enter decimal numbers and bases until they choose to stop.
4. Your program should be robust enough to handle invalid input. Valid bases that can be selected are any numbers between 2-10 & 16.
Note: You are expected to correctly handle converting base 16 numbers, using letters A-F for numbers 10-15.
base 16 digits range from 0 1 2 3 4 5 6 7 8 9 A B C D E F
here is my code:
/////////////////////////////////////////////////////////////////////////////////////////////
#include <string>
#include "stdafx.h"
#include <iostream>
using namespace std;
void PrintHeader()
{
char *BaseConversion(unsigned int , int );
int main()
{
int decNum;
int baseNum;
char again;
PrintHeader();
do
{
cout<< "Please enter a decimal num to convert : ";
cin>>decNum;
cout<< "What base would you like to convert to? 2-16? ";
cin>>baseNum;
cout<<"\n";
cout<< "Decimal num " << decNum << " converted to base num " << baseNum << " is:"<<endl;
cout<<BaseConversion(10,2);
cout<<"\n";
cout<< "Would you like to convert another num ?"<<endl;
cout<<"\n";
cout<< "Enter Y to continue and anything else to end."<<endl;
cin>> again;
cout<<"\n";
cout<<"\n";
}
//cout<<BaseConversion(10,2)<<endl;
//cout<<BaseConversion(10,3)<<endl;
//cout<<BaseConversion(10,4)<<endl;
//cout<<BaseConversion(10,5)<<endl;
//cout<<BaseConversion(10,6)<<endl;
//cout<<BaseConversion(10,7)<<endl;
//cout<<BaseConversion(10,8)<<endl;
//cout<<BaseConversion(10,9)<<endl;
//cout<<BaseConversion(10,10)<<endl;
//cout<<BaseConversion(10,11)<<endl;
//cout<<BaseConversion(10,12)<<endl;
//cout<<BaseConversion(10,13)<<endl;
//cout<<BaseConversion(10,14)<<endl;
//cout<<BaseConversion(10,15)<<endl;
//cout<<BaseConversion(10,16)<<endl;
while (again == 'y' || again == 'Y');
return 0;
}////////////////////////////end of main///////////////////////////////////
char *BaseConversion(unsigned int num, int base)
{
static char buffer[50];
char *ptr=buffer;
//Checking for invalid base input
if(base < 2 || base > 16)
return NULL;
//Going to the end of buffer
ptr = &buffer[sizeof(buffer)-1];
*ptr = '\0';
//Actual Conversion
while(num != 0)
{
*--ptr = "0123456789abcdef"[num % base];
num /= base;
}
return ptr;
}
stack.h////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef stack_H
#define stack_H
using namespace std;
class stack
{
private:
class StackNode
{
friend class stack;
int value;
StackNode *next;
//constructor
StackNode(int value1, StackNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
StackNode *top;
public:
stack()
{ top = NULL; }
void push(int);
void pop(int &);
bool isEmpty();
};
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
stack.cpp
#include "stdafx.h"
#include <iostream>
#include ".\stack.h"
#include "stack.h"
using namespace std;
//**************************************************
// Member function push pushes the argument onto *
// the stack. *
//**************************************************
void stack::push(int num)
{
top = new StackNode(num, top);
}
//************************************************** ***
// Member function pop removes the value at the top *
// of the stack and copies it into the variable *
// passed as an argument. *
//************************************************** ***
void stack::pop(int &num)
{
StackNode *temp;
if (isEmpty())
{
cout << "The stack is empty.\n";
exit(1);
}
else // pop value off top of stack
{
num = top->value;
temp = top;
top = top->next;
delete temp;
}
}
//************************************************** ***
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//************************************************** ***
bool stack::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
};
1 1330
what is the problem?
I made a few minor changes and it seems to work OK -
#include <string>
-
//#include "stdafx.h"
-
#include <iostream>
-
using namespace std;
-
-
void PrintHeader()
-
{};
-
char *BaseConversion(unsigned int , int );
-
int main()
-
{
-
int decNum;
-
int baseNum;
-
-
char again;
-
PrintHeader();
-
do
-
{
-
cout<< "Please enter a decimal num to convert : ";
-
cin>>decNum;
-
cout<< "What base would you like to convert to? 2-16? ";
-
cin>>baseNum;
-
cout<<"\n";
-
cout<< "Decimal num " << decNum << " converted to base num " << baseNum << " is:"<<endl;
-
cout<<BaseConversion(decNum, baseNum); // ** change 10 to decNum and 2 to baseNum
-
cout<<"\n";
-
cout<< "Would you like to convert another num ?"<<endl;
-
cout<<"\n";
-
cout<< "Enter Y to continue and anything else to end."<<endl;
-
cin>> again;
-
cout<<"\n";
-
cout<<"\n";
-
}
-
-
-
//cout<<BaseConversion(10,2)<<endl;
-
//cout<<BaseConversion(10,3)<<endl;
-
//cout<<BaseConversion(10,4)<<endl;
-
//cout<<BaseConversion(10,5)<<endl;
-
//cout<<BaseConversion(10,6)<<endl;
-
//cout<<BaseConversion(10,7)<<endl;
-
//cout<<BaseConversion(10,8)<<endl;
-
//cout<<BaseConversion(10,9)<<endl;
-
//cout<<BaseConversion(10,10)<<endl;
-
//cout<<BaseConversion(10,11)<<endl;
-
//cout<<BaseConversion(10,12)<<endl;
-
//cout<<BaseConversion(10,13)<<endl;
-
//cout<<BaseConversion(10,14)<<endl;
-
//cout<<BaseConversion(10,15)<<endl;
-
//cout<<BaseConversion(10,16)<<endl;
-
-
while (again == 'y' || again == 'Y');
-
-
-
-
-
-
return 0;
-
-
-
-
}////////////////////////////end of main///////////////////////////////////
-
char *BaseConversion(unsigned int num, int base)
-
{
-
static char buffer[50];
-
char *ptr=buffer;
-
-
//Checking for invalid base input
-
if(base < 2 || base > 16)
-
return NULL;
-
-
//Going to the end of buffer
-
ptr = &buffer[sizeof(buffer)-1];
-
*ptr = '\0';
-
-
//Actual Conversion
-
while(num != 0)
-
{
-
*--ptr = "0123456789abcdef"[num % base];
-
num /= base;
-
}
-
return ptr;
-
}
-
-
-
-
-
-
-
-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
//stack.cpp
-
-
//#include "stdafx.h"
-
#include <iostream>
-
//#include ".\stack.h"
-
#include "stack.h"
-
using namespace std;
-
-
//**************************************************
-
// Member function push pushes the argument onto *
-
// the stack. *
-
//**************************************************
-
void stack::push(int num) // ** added :p
-
{
-
top = new StackNode(num, top);
-
}
-
-
//************************************************** ***
-
// Member function pop removes the value at the top *
-
// of the stack and copies it into the variable *
-
// passed as an argument. *
-
//************************************************** ***
-
void stack::pop(int &num) // ** added :p
-
{
-
StackNode *temp;
-
-
if (isEmpty())
-
{
-
cout << "The stack is empty.\n";
-
exit(1);
-
}
-
else // pop value off top of stack
-
{
-
num = top->value;
-
temp = top;
-
top = top->next;
-
delete temp;
-
}
-
}
-
-
//************************************************** ***
-
// Member function isEmpty returns true if the stack *
-
// is empty, or false otherwise. *
-
//************************************************** ***
-
bool stack::isEmpty()
-
{
-
bool status;
-
if (!top)
-
status = true;
-
else
-
status = false;
-
return status;
-
};
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: George |
last post by:
Hi,
The sql query below returns data in the 'note' field from a table
called ww_rec_1 from the previous month and displays the sum of hours
for each category.
SELECT note,SUM(hours)FROM...
|
by: anon |
last post by:
Does anyone know how many man-hours were used in creating .NET 1.0?
I thought it would be an interesting statistic to know.
Thanks.
|
by: mitchchristensen |
last post by:
I have a transaction log that tracks issues from a call center. Each
time an issue is assigned to someone else, closed, etc. I get a time
stamp. I have these time stamps for the beginning of an...
|
by: Rebecca Smith |
last post by:
I'm building log book to keep track of a racers stats during a long ultra
marathon race. Things such as time on the bike, time off, H20 intake,
electrolytes and the like.
When I say long I mean...
|
by: serge |
last post by:
Right now the database I am working with is storing time in
an Integer data type and is storing the time value in seconds.
The application does not allow entering seconds. It accepts
minutes and...
|
by: Drum2001 |
last post by:
I have a table that tracks employee times. I have a column
(Date/Time).
Users, through a form, enter how long it takes them to complete a task.
For example, 03:45 = 3 hours and 45 mins.
I am...
|
by: Brett |
last post by:
Does anyone know how to calculate hours that go over 24? I am tracking how many hours & seconds our production keyers work everyday. When I run a query for a length of time that a given employee...
|
by: richbneal |
last post by:
I really like the site so far and this is my first post. I have looked through some of the archives with no luck. I have also read the posting guidelines and will do my best to be clear and accurate...
|
by: sentiald |
last post by:
I am having trouble with the following code. I need to have it calculate regular pay, overtime pay and gross pay and display it back to the user. Can anyone figure out where my problem lies.
<?php...
|
by: 22dude |
last post by:
it says the variable hours is being used with out being initializes
i am using C++ 2005 express edition , I am currently student
Please help me out to find the solution on my code below
#...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |