Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
User0015
Nov 24, 2007

Please don't talk about your sexuality unless it serves the ~narrative~!

Nippashish posted:

Nippashish talks event handling

I may have had a stroke of insight on my way home from class the other day, so tomorrow and over the weekend I'm going to try testing a few things and implementing some ideas. I may have nailed something down, but then again, maybe not. I'll sit down tomorrow and piece through all the different code I've collected so far and see if I'm right or not. Your example was incredibly helpful, as well.

Is there a good website(s) that discuss maps, function callbacks and templates? I usually visit https://www.cplusplus.com to get me started, and then browse around if that doesn't answer my questions. https://www.newty.de seems like a good place for function points and callbacks, but what about the others?

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Just FYI, cplusplus.com is one of the worst C++ sites out there.

Nippashish
Nov 2, 2005

Let me see you dance!

User0015 posted:

I may have had a stroke of insight on my way home from class the other day, so tomorrow and over the weekend I'm going to try testing a few things and implementing some ideas. I may have nailed something down, but then again, maybe not. I'll sit down tomorrow and piece through all the different code I've collected so far and see if I'm right or not. Your example was incredibly helpful, as well.

Is there a good website(s) that discuss maps, function callbacks and templates? I usually visit https://www.cplusplus.com to get me started, and then browse around if that doesn't answer my questions. https://www.newty.de seems like a good place for function points and callbacks, but what about the others?

For the stl I usually use http://www.cppreference.com/ for a quick reference and http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibref/index.html when I need an answer in detail.

As for learning templates, I learned to use them by reading the http://www.boost.org/ documentation; however, this is probably not the easiest way to go about it. Boost has pretty good documentation for most of their libraries, but they also assume you know what you're doing with templates. If you're up for it, try looking at boost::bind for a really excellent and more general version of what MemberFunctionHandler from your last post is trying to be. I'd point you to some more introductory reading, but I'm afraid I don't know any good ones.

Nippashish fucked around with this message at 05:56 on Nov 21, 2008

User0015
Nov 24, 2007

Please don't talk about your sexuality unless it serves the ~narrative~!

Avenging Dentist posted:

Just FYI, cplusplus.com is one of the worst C++ sites out there.

What would you recommend instead.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

User0015 posted:

What would you recommend instead.

Any/all of the links in my OP

User0015
Nov 24, 2007

Please don't talk about your sexuality unless it serves the ~narrative~!

Avenging Dentist posted:

Any/all of the links in my OP

I'll take a look. I honestly assumed the links you put up were for experts and professionals, since a lot of discussion and information in the entire forum is a little beyond college level. At least, that's what it looks like at first glance.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Nah, the pros just bust out their copy of ISO/IEC 14882:2003. :c00l: Legend has it that That Turkey Story learned from the ISO standard.

Jo
Jan 24, 2005

:allears:
Soiled Meat
Why is mmap always returning -1:

code:
int filePtr = open( sharedFileName, (O_CREAT|O_RDWR), (S_IRUSR|S_IWUSR|S_IRGRP) ); // Open file
_shared = (RPCShared *)mmap( NULL, sizeof(RPCShared)*maxAvailableThreads, (PROT_READ|PROT_WRITE), MAP_SHARED, filePtr, 0 );
if( (int)_shared == -1 ) {
	fprintf( stderr, "ERROR: Error mapping shared memory.  mmap returned -1 :(\n" );
	exit(-1);
}
I've tried dinking around with the options, and it doesn't matter what I do with maxAvailableThreads or RPCShared. Should I be doing something special with addr?

EDIT: Added MODE data. Still no go.

Jo fucked around with this message at 08:41 on Nov 21, 2008

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Jo posted:

Why is mmap always returning -1:

Perhaps you could print out the actual error? errno exists for a reason, you know.

Jo
Jan 24, 2005

:allears:
Soiled Meat

ShoulderDaemon posted:

Perhaps you could print out the actual error? errno exists for a reason, you know.

Ehr, yes. Right. It's errno 9.
Bad file descriptor.

EDIT: It's late, I'm probably making a stupid error opening the file or something.
Thank you for your response at this ungodly hour, ShoulderDaemon.

:doh: server1.rpc has permissions hosed up.

Jo fucked around with this message at 09:09 on Nov 21, 2008

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Jo posted:

Ehr, yes. Right. It's errno 9.
Bad file descriptor. :what:

EDIT: It's late, I'm probably making a stupid error opening the file or something.

Thank you for your response at this ungodly hour, ShoulderDaemon.

It's probably because you have a bad file descriptor. I'm betting your open call is failing and returning -1. You should check that.

Vanadium
Jan 8, 2005

Jo posted:

Ehr, yes. Right. It's errno 9.
Bad file descriptor.

EDIT: It's late, I'm probably making a stupid error opening the file or something.
Thank you for your response at this ungodly hour, ShoulderDaemon.

:doh: server1.rpc has permissions hosed up.

Ever tried out strace?

That Turkey Story
Mar 30, 2003

Avenging Dentist posted:

Nah, the pros just bust out their copy of ISO/IEC 14882:2003. :c00l: Legend has it that That Turkey Story learned from the ISO standard.

This is more fact than legend :c00lbert:

Jo
Jan 24, 2005

:allears:
Soiled Meat

Vanadium posted:

Ever tried out strace?

Never heard of it before. Looks really neat, but it's not installed on the dev server and I don't have administrative privileges.

Maxcraft
Feb 3, 2005
how can i make a function with arbitrary number of arguments, like printf?

Also how can i create function pointers to such functions? Beside casting them from void *

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Maxcraft posted:

how can i make a function with arbitrary number of arguments, like printf?

Also how can i create function pointers to such functions? Beside casting them from void *

These are called "variadic" functions. Here are some guides on implementing them:

http://www.gnu.org/software/libtool/manual/libc/How-Variadic.html
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=138
http://www.inquiry.com/techtips/cpp_pro/10min/2001/feb/10min0201-3.asp

Pointers to variadic functions are declared with the same syntax as any other function pointer.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Maxcraft posted:

how can i make a function with arbitrary number of arguments, like printf?

Also how can i create function pointers to such functions? Beside casting them from void *

Please don't do this in C++ unless you have a really good reason.

Contero
Mar 28, 2004

How do you pack files into an executable? I barely know how to do this using resource files with visual studio, but I'm looking for a more standard and platform-independent way to do this, if there is one.

What is the best way to manage a large set of constants that are going to be used throughout a program like an ini file? I want to be able to look up and modify values by a string name (like reading from a file or console) but also be able to very quickly access these values (ideally without looking them up each time I need them) throughout the program. This seems like a problem that has probably been solved thousands of times in C++ and I'd probably do a pretty awful job trying to come up with a solution on my own.

How do you work with callback functions that need to communicate without using global variables? Right now I'm doing some programming with GLUT and I really have no idea how to get things like a key callback and a timer callback to talk to each other without making every little variable I need a global. Actually, it just occurred to me I could make each callback a member of a class and talk through member variables, but what if I wanted to do this in C? I'm just curious if there are better ways to design around this problem.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Contero posted:

How do you pack files into an executable? I barely know how to do this using resource files with visual studio, but I'm looking for a more standard and platform-independent way to do this, if there is one.

There is no platform-independent way because it depends on the executable format. Windows uses PE, which is different from ELF in Linux.

Contero posted:

What is the best way to manage a large set of constants that are going to be used throughout a program like an ini file?

Boost.ProgramOptions, deserialize to a struct.

Contero posted:

How do you work with callback functions that need to communicate without using global variables? Right now I'm doing some programming with GLUT and I really have no idea how to get things like a key callback and a timer callback to talk to each other without making every little variable I need a global. Actually, it just occurred to me I could make each callback a member of a class and talk through member variables, but what if I wanted to do this in C? I'm just curious if there are better ways to design around this problem.

That's not gonna work in GLUT. You can't use member functions as function pointers (at least not in the way you want). Real mens' APIs that use callbacks let you supply a function object, and then you can do whatever you want, especially if you use the Boost/TR1 bind function.

hexadecimal
Nov 23, 2008

by Fragmaster
^^^^^^^^^^^^^^^^^^^^^

you can use a big char array in .h file. I did this to include bitmap files in my glut project. I had about 2mb .h file with all my textures as big char arrays. It made one large executable but it needed no other files to run other than itself.

you can use a map for your second problem. you can have something like map<string,int> and it would map some string to some integer value. you can have any data type as key or value.

example:
code:
#include <map>
#include <string>

std::map<std::string,int> vals;

int main()
{ 
    vals["some_name"] = 5;
}
map is implemented using black/red trees, so the look up time is not constant. You can use ext/hashmap for O(1) look up time, but you will have to make your own hash function for a string as key for it. Here is the one I have found online somewhere:

code:
#include <ext/hash_map>

// this is hash func so i can use std::string as key for hash_map later on
// i pulled it off from here: [url]http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html[/url]
namespace __gnu_cxx
{
        template<> struct hash< std::string >
        {
                size_t operator()( const std::string& x ) const
                {
                        return hash< const char* >()( x.c_str() );
                }
        };
}

__gnu_cxx::hash_map<string,int> vals;

quote:

That's not gonna work in GLUT. You can't use member functions as function pointers (at least not in the way you want). Real mens' APIs that use callbacks let you supply a function object, and then you can do whatever you want, especially if you use the Boost/TR1 bind function.

To pass functions that are member methods of class, i think you have to make them static, otherwise you cannot have function pointers to them.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

hexadecimal posted:

you can use a map for your second problem. you can have something like map<string,int> and it would map some string to some integer value. you can have any data type as key or value.

That's not going to help with serializing/deserializing the data to/from a file or reading from the command-line, which is what he was asking about, and why I suggested Boost.ProgramOptions. By way of example, I used Boost.ProgramOptions for one of my projects recently to read in a config file in INI format and it took me 20 lines of code, excluding declarations.

Also, keep in mind that the asymptotic complexity of an algorithm isn't the only measure of its speed, and depending on the amount of data, it may be easiest just to make a struct with members for every option.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
Okay, I'm doing an assignment for my C++ class. The assignment is to make a program that can encode plain text into morse code, and decode morse code into plain text. I got the first part down. It's the second part that's killing me here.

I need a way to input the initial string for morse code, then go through that string, and assign each group of characters to their own position in a string array, delimited by a blank.

So, if the input was:
code:
-... .-.. .- ....
I need a way to use something like getline to search the input, and every time there's a space, grab the preceding characters and put them in their own position in a string array. This way, once I have all the input in their own string array, I can just do a for loop to compare the positions in the input string array with the morse code string array, and convert accordingly. I was thinking about doing something like actually using getline on the input string's variable, but I don't think that would work, since I don't think a variable counts as an input stream. I also thought about declaring a string array, and then using a getline for the whole array, and delimited by a blank, but it can't convert from an array to a basic string.

Does anyone know of a way to do this? Short of falling back on going one character by one, this has me stumped.

Cosmopolitan fucked around with this message at 12:28 on Nov 23, 2008

Vanadium
Jan 8, 2005

Anunnaki posted:

I was thinking about doing something like actually using getline on the input string's variable, but I don't think that would work, since I don't think a variable counts as an input stream.

You can use an std::istringstream to wrap your string into an input stream, then you can use >> to get whitespace-delimited tokens into std::strings.

Also you could just keep calling std::string::find on the input string to find the next space, and then use std::string::substr to extract the token manually.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Contero posted:

How do you pack files into an executable? I barely know how to do this using resource files with visual studio, but I'm looking for a more standard and platform-independent way to do this, if there is one.

Qt has its own resource compiler which is much more convenient that Microsoft's, and works on Windows, Mac and Linux. You need to use Qt's QFile class to open the files you embed, but you can get that class by linking only to the QtCore module which is pretty small and ignore the rest of the GUI toolkit. (But if you're making a cross-platform app, Qt's a good choice to do the UI in anyway.)

quote:

How do you work with callback functions that need to communicate without using global variables? Right now I'm doing some programming with GLUT and I really have no idea how to get things like a key callback and a timer callback to talk to each other without making every little variable I need a global. Actually, it just occurred to me I could make each callback a member of a class and talk through member variables, but what if I wanted to do this in C? I'm just curious if there are better ways to design around this problem.

In most C systems I've seen a callback function has a void * parameter called "userdata" or something that you can pass anything you want to. So you'd pass it a pointer to a struct which has all the data members you want to communicate with. I just looked up glutTimerFunc and apparently it only lets you pass a single int, so you'd need to do something like make a global array of structs (pointed at through void pointers so you can have a different struct at each position) and use the int as an index into it. A bit ugly, but a single global array is better than a bunch of unrelated global data.

hexadecimal
Nov 23, 2008

by Fragmaster

Anunnaki posted:

Okay, I'm doing an assignment for my C++ class. The assignment is to make a program that can encode plain text into morse code, and decode morse code into plain text. I got the first part down. It's the second part that's killing me here.

I need a way to input the initial string for morse code, then go through that string, and assign each group of characters to their own position in a string array, delimited by a blank.

So, if the input was:
code:
-... .-.. .- ....
I need a way to use something like getline to search the input, and every time there's a space, grab the preceding characters and put them in their own position in a string array. This way, once I have all the input in their own string array, I can just do a for loop to compare the positions in the input string array with the morse code string array, and convert accordingly. I was thinking about doing something like actually using getline on the input string's variable, but I don't think that would work, since I don't think a variable counts as an input stream. I also thought about declaring a string array, and then using a getline for the whole array, and delimited by a blank, but it can't convert from an array to a basic string.

Does anyone know of a way to do this? Short of falling back on going one character by one, this has me stumped.

I made this little C++ function to take a string and string delimeter, and return a vector of strings that are broken around that delimeter. Also in getline you can use getline( cin, string_name, ' ' ); to make it break with spaces and not with new lines.

here is the code for my function, maybe it will help:
code:
std::vector<std::string> splitString( const std::string & input, const std::string & delimeter )
{
     std::vector<std::string> ret;
     
     size_t pos, last_pos = 0;
     while( ( pos = input.find( delimeter, last_pos ) ) != std::string::npos )
     {
            ret.push_back( input.substr( last_pos, pos - last_pos ) );
            last_pos = pos+delimeter.size();
     }
     if( last_pos < input.size() ) ret.push_back( input.substr( last_pos ) );
     return ret;
}
to use it, you can call it to vector<string> morse_letters = splitString( input, " " ); and morse_letters will be {"-...", ".-..", ".-", "...."} given that input = "-... .-.. .- ...."

hexadecimal fucked around with this message at 19:14 on Nov 23, 2008

tjl
Aug 6, 2005
I had a friend ask for help with an array program he is working on in C. I looked at the source but cannot for the life of me find why the compiler (Dev-C++ in my case) is throwing this one error.

The program is to take the int array and pass it to various functions for processing. It is "menu" driven using a while loop and a switch statement for control.

code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

void menu(void);
void sort_ascend(int A[]);
void sort_descend(void);
void str_length(void);
void palindrome(void);
void matrix_addition(void);

// main function
int main(void)
{
	int A[9];
	
	menu();
	
	return(0);
}

// menu() implementation
void menu(void)
{
	int choice = 1;
	
	// begin while loop for menu choices
	while (choice>=1 && choice<=5)
	{
      printf("Choose from the following:\n");
      printf("1. Sort numbers in ascending order.\n");
      printf("2. Sort numbers in descending order.\n");
      printf("3. Determine the length of the string.\n");
      printf("4. Determine if given string is a palindrome.\n");
      printf("5. Matrix Addition.\n");
      scanf("%d", choice);
      
      switch (choice)
      {
         case 1:
            sort_ascend(int A[]);  // line 41 this is where it breaks down
            break;
         case 2:
            sort_descend();
            break;
         case 3:
            str_length();
            break;
         case 4:
            palindrome();
            break;
         case 5:
            matrix_addition();
            break;
         default:
            printf("Wrong choice. Exiting the program.\n");
		}// end switch
	}// end while
}// end menu()

// sort_ascend implementation
void sort_ascend(int A[])
{
   int i,j, temp;

	for(i=0;i<10;i++)
	{
      printf("Enter the value for numerical array\n");
      scanf("&d", &A[i]);
	}// end for
	for(j=9;j>i;j--)
	{
      if(A[j]<A[j-1])
      {
         temp = A[j];
         A[j] = A[j-1];
			A[j-1] = temp;
		}// end if
	}// end for

	printf("The numbers in ascending order are %d.\n",temp);
}// end sort_ascend()
The compiler says:
line 41: syntax error before "int"
(which is located inside the switch statement in the above code)

I have some experience with C++, but this is puzzling me. My first thoughts were improper pass by reference or something... maybe even scope issues since there are functions outside of main(). I commented out the offending line and it compiled fine.

Any thoughts?

hexadecimal
Nov 23, 2008

by Fragmaster
you declare array A when you pass it to that function.

just pass A instead of int A[]. Also declare int A[] outside of main() where you do forward declaration of those functions. This way it would be visible in menu

code:
............

	int A[9]; // DECLARE it here so it is visible to menu function.

// main function
int main(void)
{

	
	menu();
	
	return(0);
}


// menu() implementation
void menu(void)
{
	.................


      switch (choice)
      {
         case 1:
            sort_ascend(A);  // pass reference to A that was declared globally.  What you were doing before is declaring an array 

.................
}// end menu()


hexadecimal fucked around with this message at 21:23 on Nov 23, 2008

tjl
Aug 6, 2005
Most excellent! Thank you hex, I see where it went wrong now.

crazypenguin
Mar 9, 2005
nothing witty here, move along

Anunnaki posted:

I need a way to use something like getline to search the input, and every time there's a space, grab the preceding characters and put them in their own position in a string array

string s;
cin >> s;

It stops when it reaches whitespace. I have no idea why people are writing anything more complicated?

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

crazypenguin posted:

string s;
cin >> s;

It stops when it reaches whitespace. I have no idea why people are writing anything more complicated?

Because I need it to look at the whole string, and not just up to the first blank (which would only be the equivalent of one plain-text character). Plus, if I'm not mistaken, it will corrupt the I/O buffer.

Anyway, I've been at this for hours, and I just can't figure it out. It's probably just because we haven't learned about vectors yet, so I can't see what I'm doing wrong, but this is driving me up a wall.

Here's the portion of my code that's supposed to decode morse code into plain text:

code:
int main()
{
	string morse[39] = {
				".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",
				".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-",
				".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
				"-....", "--...", "---..", "----.", "-----", ".-.-.-", "--..--", "..--.."
			 };

	string plaintext[39] = {
				"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
				"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6",
				"7", "8", "9", "0",	"stop", ",", "?"
			};

case 1: <Encode plain to morse>


case 2:
	{
		system("cls");

		valid = true;
		string outString = "", inMorseCode;
		const string delim = " ";
		int inLength; //morseArrayCounter = 0, blankCounter = 0, ifBlankCounter = 0;
		char inMorseCodeChar;

		cout << "Please enter the Plain Text you would like to convert:\n\n";
		
		cin.ignore(80, '\n');
		getline(cin, inMorseCode);

		inLength = inMorseCode.length();

		system("cls");
		cout << "You entered: " << inMorseCode;
		cout << "\n\nConverted to Plain Text:\n\n";

		for (int inMorseCounter = 0, blockCounter = 0; inMorseCounter < inLength; inMorseCounter++)
		{
			inMorseCodeChar = inMorseCode.at(inMorseCounter);

			string inBlock[] = {""};

			for (int strMorseCounter = 0; strMorseCounter < 38; strMorseCounter++)
			{
				if (inMorseCodeChar == ' ')
				{
					outString.append(delim);

					inBlock[blockCounter].append(inMorseCode.substr((inMorseCounter - blockCounter), blockCounter));
					blockCounter++;
				}

				int morseCompare = inBlock[blockCounter].compare(morse[strMorseCounter]);

				if (morseCompare = 0)
				{
					outString.append(plaintext[strMorseCounter]);
					outString.append(delim);
				}
			}
		}

		cout << outString;

		cout << "\n\nPress ANY key to continue...";
		_getch();
		system("cls");
	}
If I enter a single character, there's no output, and if I enter any spaces, it crashes. I think at this point, it's likely also to be faulty logic. Can anyone spot something I'm doing wrong? Obviously, don't write my code for me, but I'm just at a loss here.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Anunnaki posted:

Because I need it to look at the whole string, and not just up to the first blank (which would only be the equivalent of one plain-text character). Plus, if I'm not mistaken, it will corrupt the I/O buffer.

What in god's name are you talking about

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Avenging Dentist posted:

What in god's name are you talking about

cin stops after the first whitespace. I'm entering a whole string of morse code, each character separated by a space.

hexadecimal
Nov 23, 2008

by Fragmaster
code:
int main()
{
	string morse[39] = {
				".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",
				".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-",
				".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
				"-....", "--...", "---..", "----.", "-----", ".-.-.-", "--..--", "..--.."
			 };

	string plaintext[39] = {
				"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
				"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6",
				"7", "8", "9", "0",	"stop", ",", "?"
			};
			
	map<string,string> morse2plain;
	map<string,string> plain2morse;
	for( int i = 0; i < 39; ++i )
	{
         morse2plain[morse[i]] = plain[i];
         plain2morse[plain[i]] = morse[i];
    }

case 1: <Encode plain to morse>


case 2:
	{
		system("cls");

		valid = true;
		string outString = "", inMorseCode;
		const string delim = " ";
		int inLength; //morseArrayCounter = 0, blankCounter = 0, ifBlankCounter = 0;
		char inMorseCodeChar;

		cout << "Please enter the Plain Text you would like to convert:\n\n";
		
		if( cin.peek() == '\n' ) cin.ignore();
		getline(cin, inMorseCode);

		inLength = inMorseCode.length();

		system("cls");
		cout << "You entered: " << inMorseCode;
		cout << "\n\nConverted to Plain Text:\n\n";

		vector<string> morse_code_letters = splitString( inMorseCode, " " );
		for( int i = 0; i < morse_code_letters.size(); ++i )
		{
             outString.append( morse2plain[morse_code_letters[i]] );
        }

		cout << outString;

		cout << "\n\nPress ANY key to continue...";
		_getch();
		system("cls");
	}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Anunnaki posted:

cin stops after the first whitespace. I'm entering a whole string of morse code, each character separated by a space.

Based on your code, you clearly know what loops are; why wouldn't you use them?

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Avenging Dentist posted:

Based on your code, you clearly know what loops are; why wouldn't you use them?

I'm sure if I knew what you were talking about, I wouldn't be here asking for help. :confused:

hexadecimal
Nov 23, 2008

by Fragmaster

Anunnaki posted:

I'm sure if I knew what you were talking about, I wouldn't be here asking for help. :confused:

just use the code i wrote up duder. you will have to #include <map> and use the function to split strings. But it should work fine otherwise. Using maps would probably make converting from normal text to morse code a lot easier.

One thing i forgot to mention is that you probably want to know how to check if certain value is mapped or not? that is if something that user entered is really a morse code code? you can use this:

code:
if( morse2plain.find( some_string ) == morse2plain.end() ) // some_string is not in morse2plain's key set.
You can also make a useful macro for this, which i personally use a lot:

code:
#define isMapped( m, el ) (m).find((el))!=(m).end()
if( !isMapped( morse2plain, some_string ) ) // some_string is not in morse2plain key set.

hexadecimal fucked around with this message at 03:44 on Nov 24, 2008

floWenoL
Oct 23, 2002

Anunnaki posted:

cin stops after the first whitespace. I'm entering a whole string of morse code, each character separated by a space.

I'm more curious about the "corrupt the I/O buffer" part.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

hexadecimal posted:

just use the code i wrote up duder.

Yeah but your code is awful. :gonk:

floWenoL posted:

I'm more curious about the "corrupt the I/O buffer" part.

Yeah, that's what I was saying "what" about

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

hexadecimal posted:

just use the code i wrote up duder. you will have to #include <map> and use the function to split strings. But it should work fine otherwise. Using maps would probably make converting from normal text to morse code a lot easier.

I tried, but it doesn't compile. I don't get how you're defining that vector<string>. Edit: Also, I don't know what a map is. I can't find it in my textbook, and the page for it on cplusplus.com is confusing.

floWenoL posted:

I'm more curious about the "corrupt the I/O buffer" part.
Well, if you use cin to prompt input for an integer for a simple mathematical calculation, and you enter something like "2 2", it comes out garbage. Maybe I'm just using the wrong terms or something.

Cosmopolitan fucked around with this message at 03:57 on Nov 24, 2008

Adbot
ADBOT LOVES YOU

hexadecimal
Nov 23, 2008

by Fragmaster

Anunnaki posted:

I tried, but it doesn't compile. I don't get how you're defining that vector<string>. Edit: Also, I don't know what a map is. I can't find it in my textbook, and the page for it on cplusplus.com is confusing.

Well, if you use cin to prompt input for an integer for a simple mathematical calculation, and you enter something like "2 2", it comes out garbage. Maybe I'm just using the wrong terms or something.

http://www.cppreference.com/wiki/stl/map/start
here is one for the maps. Its basically a way to have an associative array. Associative array is an array where index can be anything not just numbers from 0...size of array. Think of it like a dictionary. You can map a string word to its definition that can also be a string.

for vector just make sure you #include <vector> and make sure you either do using namespace std; or using std::vector;

in the splitString function i use std:: in front of it and string to make sure that function works without having to do using std::vector; or using namespace std; before

EDIT: and oh yea, #include <map> for maps, of course.

hexadecimal fucked around with this message at 04:07 on Nov 24, 2008

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply