4 Replies - 1108 Views - Last Post: 21 October 2012 - 02:52 AM Rate Topic: -----

#1 Ryan B   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 15-October 12

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.

#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Loop structure running before running function. (C++)

#2 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

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....
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!
Was This Post Helpful? 0
  • +
  • -

#3 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

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. ;)

This post has been edited by Xupicor: 20 October 2012 - 11:26 AM

Was This Post Helpful? 1
  • +
  • -

#4 Ryan B   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 15-October 12

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
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!"
Was This Post Helpful? 1
  • +
  • -

#5 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1