Help with C++ needed...Part Two

MaxDSP

Lifer
May 15, 2001
10,056
0
71
biggest problem I have with this program is that I dont know the syntax to use for the assignment. The book says to have the user enter a four-digit number and replace EACH digit with the sum of that digit (plus seven) modulus 10. Then I have to swap first and third digits, then swap the second and fourth digits, then print the integer as 1 whole number.

Now, how do I break the four-digit number into separate digits so I can do the rest of the stuff? I was thinking that I would ask the user to enter 4 single digit numbers separately so i wouldnt have to worry about this step but I dont think the Prof wanted it to be that simple. This book is horrible at explaining some of the syntax. We had to write a program that used things like if-else statements that werent even covered until a few chapters later.
 

xyion

Senior member
Jan 20, 2001
706
0
0
hm. I remember doign a similar assignment. Look in to the floor(); function.

I could be wrong, but I do remember the floor(); function.
 

Legendary

Diamond Member
Jan 22, 2002
7,019
1
0
Use getch, get one character for every digit, then do all of your operations, if it goes beyond the ascii code for "9" subtract the ascii code for "9" then convert that integer into the character for that integer (character - <ASCII FOR NINE>) + <ASCII FOR 0> = finished character.

That's assuming that by modulus 10 you subtract 10 if it goes over 10.

char c1, c2, c3, c4;
c1 = getch();
c2 = getch();
c3 = getch();
c4 = getch();

c1 = c1 + 7;
if(c1 > '9') c1 = (c1 - int('9')) + int('0');
<REPEAT FOR C2 - C4>

swap(c1, c3);
swap(c2, c4)
cout << c1 << c2 << c3 << c4;

That should work, unless modulus means something completely off what I thought it did.
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71


<< hm. I remember doign a similar assignment. Look in to the floor(); function.

I could be wrong, but I do remember the floor(); function.
>>




<searching index...>

well, floor() is covered in the chapter right after this one but it rounds a decimal "x" to the largest integer not greater than x

floor(9.2) would be 9.0
 
Jun 18, 2000
11,221
783
126
Legendary, the modulus operation gives you the remainder of a division. For example:

5 modulus 14 = 4

5 divided into 14 will leave you a remainder of 4.

Maxdsp, this is what you'll need to do:

1) Declare a variable of type apstring.
2) Have the user enter in his number.
3) Now, you have the number stored in the asptring. You can easily access the individual characters in the string by using the index operator "[]".
4) Now, do all of the necessary calculations and data manipulation.
5) Output the resulting string.

Give me a minute and I can type out more detailed syntax.
 
Jun 18, 2000
11,221
783
126
You can break up the code into separate functions if you want. This is just a quick typeout of how it will work.

int main() {
          apstring userString, tempString;

          cout<< "Enter your 4 digit number: "; // The user types in his number.
          cin>> userString; // The user THINKS he's entering in a numeric value, but behind the scenes the data is going into a string variable.

          for(int x = 0; x < 4; x++)
                    userString[x] = (atoi(userString[x]) + 7) % 10;

          tempString = userString[1];
          userString[1] = userString[3];
          userString[3] = tempString;

          tempString = userString[2];
          userString[4] = userString[2];
          userString[2] = tempString;

          cout<< "The resulting number is: " << userString;

          return 0;
}

Well, something like that. :)
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71


<< You can break up the code into separate functions if you want. This is just a quick typeout of how it will work.

int main() {
          apstring userString, tempString;

cout<< "Enter your 4 digit number: "; // The user types in his number.
cin>> userString; // The user THINKS he's entering in a numeric value, but behind the scenes the data is going into a string variable.

for(int x = 0; x < 4; x++)
userString[x] = (atoi(userString[x]) + 7) % 10;

tempString = userString[1];
userString[1] = userString[3];
userString[3] = tempString;

tempString = userString[2];
userString[4] = userString[2];
userString[2] = tempString;

cout<< "The resulting number is: " << userString;

}

Well, something like that.
>>




I appreciate the help KnightBreed but I dont think the Prof. is gonna like that. APstring isnt even covered in this crappy boks for some reason. Anyway, I found out that the stuff he wnted us to use was the getchar function, which is like Chapter 17 and were on Chapter 2 right now.

Do I use getchar() or getc()? The book isnt going in-depth for this
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Well, occassionaly they don't like getch().

I would do it this way:

int x,d1,d2,d3,d4;
char string[15]; // slightly oversized, but whatever

printf("Enter four digit number :");
scanf("%s",&string);
x = atoi(string);
while ((x<1000)||(x>9999))
{
printf("\nPlease enter a FOUR digit number :");
scanf("%s",&string);
x = atoi(string);
}
d1 = 0x30 + string[0]; // 0x30 is the hex code for the character '0', string[0] is the first digit, since I know there is no minus sign
d2 = 0x30 + string[1];
d3 = 0x30 + string[2];
d4 = 0x30 + string[3];

d1 = (d1 +7) % 10;
d2 = (d2 +7) % 10;
d3 = (d3 +7) % 10;
d4 = (d4 +7) % 10;

x = d3*1000 + d4*100 + d1 * 10 + d2; // sum them up the way we need it
printf ("\n\nThe result is: %d",x);

Okay, so the whole thing is written in C, but whatever.
This'll work for ya.

Edit: changed <0 into <1000 , since we want a four digit number
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
to break the 4 digit number up into 4 variables, just divide the 4 digit number by 1000, then by 100, then by 10, and the remaining number will be your last number. For example, 5849...5849/1000 = 5(rounded). Remainder is 849. 849/10 0 = 8(rounded). Remainder is 49. 49/10 = 4(rounded) and you are left with 9. Sounds complex, but it's pretty simple.
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71
Well, since the damn Prof. didnt specifiy any restrictions on the methods we can use, I took the simple approach after much debating with myself. Heres the final code Im gonna turn in:#include <iostream>

using std::cout;
using std::cin;

int main(void)

{

int c1, c2, c3, c4; //Variables for 4 digits
cout<<"Please enter 4 separate digits to encrypt.\n\n";

cout<<"Please enter 1st digit: "; //User enters 4 digits to make a single 4-digit integer
cin>>c1;
cout<<"Please enter 2nd digit: ";
cin>>c2;
cout<<"Please enter 3rd digit: ";
cin>>c3;
cout<<"Please enter 4th digit: ";
cin>>c4;

c1=(c1+7)%10; //Takes value of each digit, adds 7, and mods 10, and replaces original value
c2=(c2+7)%10;
c3=(c3+7)%10;
c4=(c4+7)%10;

cout<<"\n\n"<<c3<<c4<<c1<<c2; //Prints out swapped digits, 1st and 3rd, 2nd and 4th
cout<<"\n\n";



return (0);

}

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
the entire thing in perl:

$num = <>;
@num = split //,$num;
for($i=0;$i<4;$i++){$num[$i] = ($num[$i] + 7) % 10)}
print "$num[2]$num[3]$num[0]$num[1]";



Have fun with C++ :)
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71


<< to break the 4 digit number up into 4 variables, just divide the 4 digit number by 1000, then by 100, then by 10, and the remaining number will be your last number. For example, 5849...5849/1000 = 5(rounded). Remainder is 849. 849/10 0 = 8(rounded). Remainder is 49. 49/10 = 4(rounded) and you are left with 9. Sounds complex, but it's pretty simple. >>




WOOOOOHOOOOOOOOO! I got it. Thanks a lot XZeroII, I modified the code I was gonna turn in with the hints you gave me and it worked. I think this is the way he wanted us to do it. Thanks....now I have to do the opposite. Shouldnt be too hard

Heres the code Im gonna turn in after I took XZeroII's advice: :D
#include <iostream>

using std::cout;
using std::cin;

int main(void)

{

int num, c1, c2, c3, c4; //Variables for 4 digits

cout<<"Enter a 4-digit integer to encrypt: ";
cin>>num;

cout<<"\n\n";
c1=num/1000; //Separates first digit

num=num%1000; //Separates second digit
c2=num/100;

num=num%100; //Separates third digit
c3=num/10;

num=num%10; //Separates fourth digit
c4=num;


c1=(c1+7)%10; //Takes value of each digit, adds 7, and mods 10, and replaces original value
c2=(c2+7)%10;
c3=(c3+7)%10;
c4=(c4+7)%10;

cout<<"\n\n"<<c3<<c4<<c1<<c2; //Prints out swapped digits, 1st and 3rd, 2nd and 4th
cout<<"\n\n";



return (0);

}

 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
what in the hell are you doing?


just use modulus to find out what digit is in what place

assuming he's writing his numbers in base 10 just use modulus and division to find out what each number is


i.e

int input = 3456; //say you get this number form input use cin to get it from the user in one block
int ouput = 0;

int firstDigit = ((input % 10) + 7) % 10; //gets ones digit
int secDigit = ((input % 100)/10 + 7) % 10; //gets tens digit
int thirdDigit = ((input % 1000)/100 + 7) % 10; //gets hundreds digit
int fourthDigit = ((input % 10000)/1000 + 7) % 10; //get thousands digit


// i am assuming the first digit is the ones digit and 4th is the thousands

// output should change input from 4 - 3 - 2 - 1 ==> 2 - 1 - 4 - 3


output = (output * 10) + secDigit;
output = (output * 10) + firstDigit;
output = (output * 10) + fourthDigit;
output = (output * 10) + thirdDigit;


violioa your done out is the number input with all the digits summed with seven modulo 10 and the the 1st and 3rd flipped and the second and fourth flipped.





do not turn in what you have the prof most definelty doesnt want that.







 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
I think that the way you are doing your program is way too complicated. If you used an array to store the numbers, it would be pretty easy and very straightforward...
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71
dang, now Im having trouble with decrypting the number. Not as easy as I thought. How do I do the opposite of the modulus?

Thats is, 3591 is 2468 encrypted. I cant do something like char1=(char1-7)%10...already tried without success.


BTW, Ameesh, are you talking about not turning in the first or the second one? The second one is similar to what you suggested, which is the one Im turning in.

cchen, actually this seems easier to me since we haven't gotten to arrays yet(and I forgot everything about them from high school). Thats like 4-5 weeks later into the term.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0


<< I think that the way you are doing your program is way too complicated. If you used an array to store the numbers, it would be pretty easy and very straightforward... >>



my way is best :p
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
either, in the second you will get marked off most likely for asking for each digit. i just showed you how to get each individual digit out a single 4 digit number with converting it into a string and back to a number, it stays a number the whole time
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71


<< I still like mine best. C++ isn't the best tool for everything all the time, you know ;) >>




Punch cards, anyone?


:D
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0


<< I still like mine best. C++ isn't the best tool for everything all the time, you know ;) >>



his class is in C++ though, he cant turn in an assignment in perl, plus mine is probably half the instructions yours is. if he had to run the program a million times or so it would make a huge difference in time
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0


<< his class is in C++ though, he cant turn in an assignment in perl, plus mine is probably half the instructions yours is. if he had to run the program a million times or so it would make a huge difference in time >>


BTW, C++ is object oriented. This program is not. It's C, except for the use of the streaming operators and cin and cout.
But I guess that's a m00t point.
 

GermyBoy

Banned
Jun 5, 2001
3,524
0
0


<< I think that the way you are doing your program is way too complicated. If you used an array to store the numbers, it would be pretty easy and very straightforward... >>



Although XZeriII gives good advice in this case, this would be much more compact. You just need a somple for loop.

int I[4];

Then you can access I[0] -> I[3] and do whatever operations you want to.