d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Problem > Hoping For A Solution
12Next
Add Reply New Topic New Poll
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Sep 26 2012 08:08pm
Alright

Background: I'm in a intro to c++ class for an engineering degree requirement and the teacher acts like we are already experts in c++ and assumes we know a lot more than shes taught in the lectures or readings.

Problem: We have to display the character value in dec, oct, and hex forms from the user entering to characters ( she provided us with knowledge of the ASCII tables just for testing purposes. Now were supposed to use
static_cast,s to temp change the char variable to a char. The user must input two values so if it was an A and a D it would have to display the dec oct and hex for each the A and the , we also have to print to display the
characters that are inbetween A and D and they must have to go in reverse order also depending on how the user inputs them. She mentioned we might have to use a "while" loop? I do believe its something of this sort

Code

while
{
    (char1++)
}


to display the next letter after A and it would be char1+- if the user entered D first so it would go in reverse?

This is what I have so far if anyone could help me that would be amazing!

Code

#include <iostream>
#include <cmath>
using namespace std;

int main(void)
{
//Variable Declarations
   
char char1;
char char2;



cout << "Please enter the first of two characters: ";
cin  >> char1;

cout << "Please enter the second of the two characters: ";
cin  >> char2;
cout << endl<<endl;

//Row 1 Headers
cout.width(15);
cout << "Character";

cout.width(15);
cout << "Decimal";

cout.width(15);
cout << "Octal";

cout.width(20);
cout << "Hexadecimal";
cout << endl<<endl;

//Row 2 char 1 values
cout.width(11);
cout << char1;

cout.width(16);
cout << dec << static_cast<int>(char1);

cout.width(17);
cout << oct << static_cast<int>(char1);

cout.width(16);
cout << hex << static_cast<int>(char1);
cout << endl;


She mentioned that we only had to get it to work for the first character and then once we set up the loop correctly we could have it do the second character along with the ones inbetween.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 27 2012 12:35am
Uhm... I could see using a for loop.

Also, why you creating 2 chars? This is C++ right? Use that string library IMO.

Throw the user inputs into a string. After user input is finished, use various string functions to get some info. Length comes to mind (for the for loop). Use the for loop to go thru the string and fetch the values. Make functions or something to deal with the

Quote
we also have to print to display the
characters that are inbetween A and D and they must have to go in reverse order also depending on how the user inputs them


Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
Sep 27 2012 03:07am
With a little bit of maths knowledge, this is really not difficult at all. The way I see this it's more a maths problem than a C++ problem.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 27 2012 07:43am
id just use the itoa function with a while loop.

some psudo C code to get your brain working

Code
int main()
{
char output[25];
int first, second;

printf("enter first char: ");
scanf("%d", &first);

printf("\nenter second char: ");
scanf("%d", &first);

while(first <= seccond)
{
  itoa(first, output, 10);
  printf("\ndecimal: %s\n", output);

  itoa(first, output, 16);
  printf("Hexidecimal: %s\n", output);


  itoa(first, output, ???);  //forgot the base to oct
  printf("octl: %s\n", output);

  first++;
}
return 0;
}


that code is in C and not C++. it is using C functions and is not suitable for your homework. its just psudo code to see how the while loop may be structured.

this is also bad code because it assumes first is ALWAYS less than second and uses it without doing any user input checking.

basically what you want to do is, while the first letter is less than the second (you may need to cast them to integer for doing the checks depend how you set them up), you do your math or use a c++ function to change the base of the decimal of the inputted character, then print it out and then increment the first char. ex: first char was 'a' (97) and you increment it it would become 'b' (98)


edit:: changing the base of a decimal is easy via math just google it. there was no need to use the itoa function on my half but i didnt want to give you everything.

This post was edited by AbDuCt on Sep 27 2012 07:45am
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Sep 27 2012 08:20am
I don't know C what so ever, and the way that I have the program coded so far is how were "supposed" to be doing it, I just have to figure out how to have it loop back to the first display of the first character and make it go in consecutive order to the second character the user entered.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Sep 27 2012 08:26am
If you want conversions to base 8 or 16 in C++, you don't need to write your own functions.

Code
#include <iostream>

using namespace std;

int main()
{
int a = 17;
cout << "base  8: " << oct << a << endl;
cout << "base 10: " << dec << a << endl;
cout << "base 16: " << hex << a << endl;

}


http://ideone.com/trjq9
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Sep 27 2012 08:38am
Idk if you read but I have to use the static_cast because the user is inputting a char not a int.
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Sep 27 2012 09:37am
Code
#include <iostream>
#include <cmath>
using namespace std;

int main(void)
{
//Variable Declarations
   
char char1;
char char2;



cout << "Please enter the first of two characters: ";
cin  >> char1;

cout << "Please enter the second of the two characters: ";
cin  >> char2;
cout << endl<<endl;

//Row 1 Headers
cout.width(15);
cout << "Character";

cout.width(15);
cout << "Decimal";

cout.width(15);
cout << "Octal";

cout.width(20);
cout << "Hexadecimal";
cout << endl<<endl;

//Row 2 char 1 values
cout.width(11);
cout << char1;

cout.width(16);
cout << dec << static_cast<int>(char1);

cout.width(17);
cout << oct << static_cast<int>(char1);

cout.width(16);
cout << hex << static_cast<int>(char1);
cout << endl;

while(char1>=char2)
{
cout.width(11);
cout << char1;

cout.width(16);
cout << dec << static_cast<int>(char1++);

cout.width(17);
cout << oct << static_cast<int>(char1++);

cout.width(16);
cout << hex << static_cast<int>(char1++);
cout << endl;
}



THis is what I have so far...
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 27 2012 08:17pm
Quote (Speed93 @ Sep 27 2012 10:38am)
Idk if you read but I have to use the static_cast because the user is inputting a char not a int.


i dont think you understand how casting works.

you can use his code and instead of using an integer use a char datatype and cast it like you are already doing.

Quote (Speed93 @ Sep 27 2012 11:37am)


THis is what I have so far...


and...? whats your problem now.
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Sep 27 2012 09:10pm
How do I get it to print the characters between the two the user inputs?
And its only printing the first line of the characters

This post was edited by Speed93 on Sep 27 2012 09:24pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll