#include <iostream> //For cin and cout
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
void encryptaFile();
void decryptaFile();
void exit();
int getKeyMap(int x[128]);
int main(void)
{
int selection = 0;
cout << "This program encripts and decrypts a text based file!" << endl;
while(selection != 3)
{
selection = 0;
//Display menu
cout << "1. Encrypt a file" << endl;
cout << "2. Decrypt a file" << endl;
cout << "3. Exit" << endl;
//Get selection
cin >> selection;
switch(selection)
{
case(1):
{
int input[128];
int keyMap = getKeyMap(input);
break;
}
case(2):
decryptaFile();
break;
case(3):
exit();
break;
default:
cout << "Please enter a valid selection!\n";
if(cin.fail())
{
cin.clear();
cin.ignore(100, '\n');
}
break;
}
}
//The next two lines stop the Command Prompt window from closing
// until the user presses a key, including Enter.
cout << "Press any key to exit." << endl;
cin.ignore(2);
return 0;
}
/*
void encryptaFile()
{
int y[128];
int z = getKeyMap(y);
cout << z;
}
*/
int getKeyMap(int map[])
{
char key[128];
int length = 0;
cin.get(key, 128);
length = strlen(key);
for(int i = 0; i < strlen(key); i++)
{
int x = static_cast<int>(key[i]) - static_cast<int>('a');
map[i] = x;
}
return length;
}
void decryptaFile()
{
cout << "Decrypt a File" << endl;
}
void exit()
{
cout << "Exiting" <<endl;
}
4 Replies - 1108 Views - Last Post: 21 October 2012 - 02:52 AM
#1
Problem with Loop structure running before running function. (C++)
Posted 20 October 2012 - 10:06 AM
My problem is that my switch structure isn't going to my function when I select 1. I would google this but I'm not sure what to search. As far as I can tell the switch statements are correct. I've also tried using a while loop with if else statements and it did the same thing.
Replies To: Problem with Loop structure running before running function. (C++)
#2
Re: Problem with Loop structure running before running function. (C++)
Posted 20 October 2012 - 10:47 AM
You can't do it this way, trying to pass array to the function with it's size....
What you have to do is, pass the size of the array in a different argument.
Hope this Helps!
10 int getKeyMap(int x[128]);
What you have to do is, pass the size of the array in a different argument.
int getKeyMap(int x[], int size);
Hope this Helps!
#3
Re: Problem with Loop structure running before running function. (C++)
Posted 20 October 2012 - 11:23 AM
While that's true, it's also true that this size (int x[128]) is not going to change a thing, it's ignored as far as i remember. I'm not in a mood to search through docs on this one, maybe later. 
OPs immediate problem is a variation of the old dangling \n, if you observe the program's output you will notice that actually, it does work. getKeyMap() is called and in it, cin.get(key, 128) is called, which gets a dangling \n and... The rest is silence.
OPs immediate problem is a variation of the old dangling \n, if you observe the program's output you will notice that actually, it does work. getKeyMap() is called and in it, cin.get(key, 128) is called, which gets a dangling \n and... The rest is silence.
This post has been edited by Xupicor: 20 October 2012 - 11:26 AM
#4
Re: Problem with Loop structure running before running function. (C++)
Posted 20 October 2012 - 12:07 PM
Thank you Xupicor. I was about to send you a message asking about where the '\n' was but I searched it first and found this: http://www.cplusplus...eginner/49571/. I just added a 
That was a real "gotcha!"
cin.get()before my
cin.get(key, MAX_LENGTH);and it worked perfectly!Now I have to finish the rest of it. It's due tonight.
That was a real "gotcha!"
#5
Re: Problem with Loop structure running before running function. (C++)
Posted 21 October 2012 - 02:52 AM
Very nice of you to actually search before asking and share the solution. Commendable.
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|