d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Easy C++ Question
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Sep 16 2014 11:47am
Just starting out...



4C: A little cryptography

write a c++ program that hides a message in five words. use one of the characters in the five input strings to spell out a new word. make up atleast one other message besides this one;

enter five words: cheap energy can cause problems
enter five intergers: 4 2 1 0 5
secret message: peace

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

my code
Code
int main()
{
string a = "cheap ";
string b = "energy ";
string c = "can ";
string d = "cause ";
string e = "problems ";
string longString(a + b + c + d + e);
int w = 2

cout << "The sentance is: "<< longString << endl;
cout <<
cout << longString[4];
// P
cout << longString[2];
// E
cout << longString[3];
// A
cout << longString[0];
// C
cout << longString[2] << endl;
// E



Am i doing this write? this is like chapter 3 of the class so i think the teacher is aiming for simplicity even though it is redundant coding..
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Sep 16 2014 12:54pm
Do you not have a compiler handy? Your code doesn't even compile.

Your idea of displaying characters is also incorrect, you can't just concatenate the words into one string.

Since 5 words were mentioned, you're probably expected to use an array of words, e.g.:
Code
std::array<string,5> words;


Then you need to store 5 integers into a similar array and then simply display them correctly.

Here's a way over the top implementation:
Code
#include <array>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <utility>

using namespace std;

int main()
{
array<string,5> words;
array<int,5> integers;

cout << "Enter words: " << endl;
copy_n(istream_iterator<string>(cin), 5, begin(words));
cout << "Enter secret integers: " << endl;
copy_n(istream_iterator<int>(cin), 5, begin(integers));

transform(begin(words), end(words), begin(integers),
ostream_iterator<char>(cout),
[](string const& s, int n){
return s[n];
});
}


http://melpon.org/wandbox/permlink/9zCv5NZ8hnBeEiy2

This post was edited by KrzaQ2 on Sep 16 2014 12:54pm
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Sep 16 2014 04:27pm
Oh yea that was older code mid project but yea it complied without the blank cout << but I think you're explanation is too complex for where I'm at and my teacher would suspect.
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Sep 17 2014 12:22am
Quote (Trig @ Sep 17 2014 12:27am)
Oh yea that was older code mid project but yea it complied without the blank cout << but I think you're explanation is too complex for where I'm at and my teacher would suspect.


He did that on purpose, you have to get the general idea and make up your own program

This post was edited by m0hawk on Sep 17 2014 12:22am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 18 2014 08:25pm
Quote (m0hawk @ Sep 17 2014 02:22am)
He did that on purpose, you have to get the general idea and make up your own program


Kind of counter intuitive if you yell techno babble at someone who can't speak yet. I would of done the POC in a different language on a dumbed down skill level if he wanted this effect.
Retired Moderator
Posts: 18,570
Joined: Feb 27 2004
Gold: 11,858.00
Trader: Trusted
Sep 18 2014 09:43pm
Try reading up on string::at

Documentation is here: http://www.cplusplus.com/reference/string/string/at/

Code
cout << a.at(4);


That should give you 'p' from 'cheap'.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Sep 19 2014 01:59am
Quote (AbDuCt @ 19 Sep 2014 04:25)
Kind of counter intuitive if you yell techno babble at someone who can't speak yet. I would of done the POC in a different language on a dumbed down skill level if he wanted this effect.
You can easily change those algorithms to loops. Tbh, this looks a lot like pseudocode he'd be expected to produce at first.
1. fill words array
2. fill integers array
3. iterate those arrays in lockstep, from each word take letter at index of corresponding integer
4. print the result

Quote (Blind[zF&#93; @ 19 Sep 2014 05:43)
Try reading up on string::at

Documentation is here: http://www.cplusplus.com/reference/string/string/at/

Code
cout << a.at(4);


That should give you 'p' from 'cheap'.
Why not use operator[]? a[4] instead of a.at(4) is easier and there's little difference between them for a novice.

Retired Moderator
Posts: 18,570
Joined: Feb 27 2004
Gold: 11,858.00
Trader: Trusted
Sep 19 2014 06:03am
Quote (KrzaQ2 @ Sep 19 2014 02:59am)
You can easily change those algorithms to loops. Tbh, this looks a lot like pseudocode he'd be expected to produce at first.
1. fill words array
2. fill integers array
3. iterate those arrays in lockstep, from each word take letter at index of corresponding integer
4. print the result

Why not use operator[]? a[4] instead of a.at(4) is easier and there's little difference between them for a novice.


You're right, there is very little difference. Especially since he is hard coding the position values he is looking for in the strings.

Operator is a bit faster but does no checking if the spot referenced is even in the length of the string. Where as string::at will check the length first and throw back a nice 'out of bounds'.

Again, either will be fine in this case.

I am simply trying to point him in the right direction without giving him the entire solution.

This post was edited by Blind[zF] on Sep 19 2014 06:05am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll