473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Due in 2 hours

13
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;
};
Dec 16 '06 #1
1 1330
horace1
1,510 Expert 1GB
what is the problem?

I made a few minor changes and it seems to work OK
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. //#include "stdafx.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. void PrintHeader()
  7. {};
  8. char *BaseConversion(unsigned int , int );
  9. int main()
  10. {
  11. int decNum;
  12. int baseNum;
  13.  
  14. char again;
  15. PrintHeader();
  16. do
  17. {
  18. cout<< "Please enter a decimal num to convert : ";
  19. cin>>decNum;
  20. cout<< "What base would you like to convert to? 2-16? ";
  21. cin>>baseNum;
  22. cout<<"\n";
  23. cout<< "Decimal num " << decNum << " converted to base num " << baseNum << " is:"<<endl;
  24. cout<<BaseConversion(decNum, baseNum);  // ** change 10 to decNum and 2 to baseNum
  25. cout<<"\n";
  26. cout<< "Would you like to convert another num ?"<<endl;
  27. cout<<"\n";
  28. cout<< "Enter Y to continue and anything else to end."<<endl;
  29. cin>> again;
  30. cout<<"\n";
  31. cout<<"\n";
  32. }
  33.  
  34.  
  35. //cout<<BaseConversion(10,2)<<endl;
  36. //cout<<BaseConversion(10,3)<<endl;
  37. //cout<<BaseConversion(10,4)<<endl;
  38. //cout<<BaseConversion(10,5)<<endl;
  39. //cout<<BaseConversion(10,6)<<endl;
  40. //cout<<BaseConversion(10,7)<<endl;
  41. //cout<<BaseConversion(10,8)<<endl;
  42. //cout<<BaseConversion(10,9)<<endl;
  43. //cout<<BaseConversion(10,10)<<endl;
  44. //cout<<BaseConversion(10,11)<<endl;
  45. //cout<<BaseConversion(10,12)<<endl;
  46. //cout<<BaseConversion(10,13)<<endl;
  47. //cout<<BaseConversion(10,14)<<endl;
  48. //cout<<BaseConversion(10,15)<<endl;
  49. //cout<<BaseConversion(10,16)<<endl;
  50.  
  51. while (again == 'y' || again == 'Y');
  52.  
  53.  
  54.  
  55.  
  56.  
  57. return 0;
  58.  
  59.  
  60.  
  61. }////////////////////////////end of main///////////////////////////////////
  62. char *BaseConversion(unsigned int num, int base)
  63. {
  64. static char buffer[50];
  65. char *ptr=buffer;
  66.  
  67. //Checking for invalid base input
  68. if(base < 2 || base > 16)
  69. return NULL;
  70.  
  71. //Going to the end of buffer
  72. ptr = &buffer[sizeof(buffer)-1];
  73. *ptr = '\0';
  74.  
  75. //Actual Conversion
  76. while(num != 0)
  77. {
  78. *--ptr = "0123456789abcdef"[num % base];
  79. num /= base;
  80. }
  81. return ptr;
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  91. //stack.cpp
  92.  
  93. //#include "stdafx.h"
  94. #include <iostream>
  95. //#include ".\stack.h"
  96. #include "stack.h"
  97. using namespace std;
  98.  
  99. //**************************************************
  100. // Member function push pushes the argument onto *
  101. // the stack. *
  102. //**************************************************
  103. void stack::push(int num)  // ** added :p
  104. {
  105. top = new StackNode(num, top);
  106. }
  107.  
  108. //************************************************** ***
  109. // Member function pop removes the value at the top *
  110. // of the stack and copies it into the variable *
  111. // passed as an argument. *
  112. //************************************************** ***
  113. void stack::pop(int &num)  // ** added :p
  114. {
  115. StackNode *temp;
  116.  
  117. if (isEmpty())
  118. {
  119. cout << "The stack is empty.\n";
  120. exit(1);
  121. }
  122. else // pop value off top of stack
  123. {
  124. num = top->value;
  125. temp = top;
  126. top = top->next;
  127. delete temp;
  128. }
  129. }
  130.  
  131. //************************************************** ***
  132. // Member function isEmpty returns true if the stack *
  133. // is empty, or false otherwise. *
  134. //************************************************** ***
  135. bool stack::isEmpty()
  136. {
  137. bool status;
  138. if (!top)
  139. status = true;
  140. else
  141. status = false;
  142. return status;
  143. };
  144.  
Dec 16 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
14
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.
5
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...
5
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...
1
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...
22
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...
1
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...
6
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...
3
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...
1
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 #...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.