Grn Xtrm 84 Posting Pro in Training

Try reading this on array intialization
http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html
It seems to me that you declare the array 'm' with an inital size of 1, but are trying to add more than one element to the array. I would give a constant size to the array that will be big enough to accomodate a substantial input. Also you must set the array = to the values which must be stored in curly braces{}. Refer to the link if my explanation was unclear.

Grn Xtrm 84 Posting Pro in Training

Nice. How long you been playing?

I just got my Hofner about a month ago, but I've been playing bass on and off for about 10 years. In recent years, its been only on. And now with the formation of my new band, I'm more determined than ever to practice everyday. Currently I am learning Hysteria by Muse, just to show you where my skill level is at ;) Not to brag or anything. :D
What about you, how long have you been playing jbennet?

Grn Xtrm 84 Posting Pro in Training

I would bring my Hofner bass, my laptop (also with infinite battery, because that's possible ;) ) , my iPod (I guess that needs infinte battery life as well) and my cell phone with... you guessed it infinite battery life ;)

Grn Xtrm 84 Posting Pro in Training

Have you tried using a for loop to print each element of the array? That's how I usually go about displaying the contents of an array.

Grn Xtrm 84 Posting Pro in Training

Function definitions must appear out of the main(). I like to put the function prototypes first. Then open main, call functions, and close main. Then write the function definitions after main. Line 11 is also a problem. You don't include a data type for a function call. Also remember that void functions have no return value. Your current code is very confusing to me due to the functions being declared in main. Also remember to keep your parameters consistent. The function prototype, call, and definition must all have the same number of parameters, and they must be of corresponding data types. Also, you don't set values for the variables you include as parameters in the function prototype. (line 5) Try to resolve these problems before you tackle the rest of the program.

Grn Xtrm 84 Posting Pro in Training

To ask the user to enter the size of an array use:

int size;
cout<<"enter size of the array";
cin>>size;

You can then use a for loop to have the user enter the values of the array.

Grn Xtrm 84 Posting Pro in Training

The parameter in the function is different from the parameter in the function call. To be more specific, the call has an array as the parameter, while the definition has an int value that is not an array. They must be the same data type.
But if I may offer my personal take on the matter:
I would make the function a void function with no parameters. Your current function is also wrong because it is declared as int but does not return an int value. Void solves this problem because a void function does not return any values.
Here is what I had in mind.

#include <iostream>
using namespace std;
 //Function prototype
void  fillArray();
int main()
{
    fillArray();
    return 0;

 }  //End main fn
 
 //****************************************************
 //Function definition
void fillArray()
{
     const int SIZE = 3;
     int num[SIZE];
     for (int i=0; i < SIZE; i++)
         cin   >> num[i];
     for (int i=0; i < SIZE; i++)
         cout   << num[i]; 
 }  //End fillArray fn.

The function creates the array, fills it, and outputs its contents. Let me know if you need any clarification.

Grn Xtrm 84 Posting Pro in Training

following up on the post by thines01 :
This will print the string in double quotes

string str2 = "\"Hello\"";
cout<<str2;

The output of the above code is
"Hello"

Grn Xtrm 84 Posting Pro in Training

The first double quote before the second Hello are causing the string to terminate. You can resolve this by putting single quotes instead of double quotes.

Grn Xtrm 84 Posting Pro in Training

You need to ask a question if you expect to get any help. Tell us specifically where you are encountering problems. Also, please use code tags to make the code more readable.

Grn Xtrm 84 Posting Pro in Training

Actually, I was the first abuser of the voting system. I down voted every single post by The Mad Hatter. So...way to be wrong ;)

Yeah, you and everyone else who read that thread ;)

nav33n commented: :D +0
Grn Xtrm 84 Posting Pro in Training

Don't quit. Just limit your posts to pieces of advice that you think will be beneficial to the OP. Don't post for the reputation and solved thread count, post because you want to help people with their problems. I havent read any of your alleged bad posts, so I dont want to judge, but don't just post for the sake of posting. Remember, some things are better left unsaid. If you don't know something, mind your place and let someone else answer.

Grn Xtrm 84 Posting Pro in Training

Peanut butter M&M's. These are the best variety of M&M's in my opinion.

Grn Xtrm 84 Posting Pro in Training

I don't know what an employee pay revision list should consist of either. My guess would be the employee's old salary and the new revised salary. This should have been described in the assignment. Is there anything in the assignment description that can give some more information?

Grn Xtrm 84 Posting Pro in Training

Sorry about the 'he' in my previous post. I should have realized Leena is a female name. My apologies.
Anyway, what else does this assignment consist of? We can't just give you the source code for an assignment without you showing the slightest understanding of the material. You should read the forum rules before starting new threads. You must show some effort and ask a clear, coherent question in order to warrant a helpful response from the rest of the community.

Grn Xtrm 84 Posting Pro in Training

tkud, you should give mention to the author of the code snippet. In the future, it would probably be better to provide a link to the code snippet this way the author is given his or her much deserved credit.

Grn Xtrm 84 Posting Pro in Training

He wants someone to do the assignment ;)
The question doesnt even make any sense. We have no idea what the employee pay revision list is. We need more info and you need to show some effort.

Grn Xtrm 84 Posting Pro in Training

Lines 3 and 11. No semicolons after if statements.

Grn Xtrm 84 Posting Pro in Training

The cpp files are gonna be the ones with the executable code but the others may be needed to run the cpp files. Maybe the code uses functions defined in the header (.h) files. Start by downloading and compiling the .cpp files and see if any of them include any other files.

Grn Xtrm 84 Posting Pro in Training

Final Fantasy XI already exists. Do you really want to remake an already existing game ... and why?

Grn Xtrm 84 Posting Pro in Training

Download a compiler such as Dev C++. Save the source files, then open them in the compiler, and compile them.

Grn Xtrm 84 Posting Pro in Training

If you want to use a letter. make the sure the menu selection variable is a char data type. Without seeing the code, this is my best guess on what the problem may be. Post the code so we can see for sure.

Grn Xtrm 84 Posting Pro in Training

Thank you very much, that was very helpful.

Grn Xtrm 84 Posting Pro in Training

probably you want to search for the first word(Secret) in all line
you can modify so easily.

Thanks for the reply. That however is not really what I'm looking for. Let me clarify. I want to search all the lines for the first word in the file. The code you supplied counts the first word of each line in that specific line. I have tried putting the functrion call in the while loop prior to your post and then just moved it back out when I posted. The above quote is actually what I'm looking for. I have tried to edit the code but have been unsuccessful. Would you mind giving me some more advice? Thanks.

Grn Xtrm 84 Posting Pro in Training

I got my function to work somewhat but it only prints the number of occurrences of the first word of the last line in the last line only. I just want to get the program to search all lines for the first word of the txt file.
Here is the revised code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000
#define SIZE 100

void search_string(char *file);

int main()
{
    int counter = 0;
    const char filename[] = "in.txt";        
    FILE *file = fopen(filename, "r");    
    if ( file != NULL )
    {
     char line [ MAX ]; 

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
            ++counter;
            printf( "%d  ", counter);
            fputs ( line, stdout ); /* write the line */
      }
      printf( "\n\n");
      search_string(line);
      fclose ( file );
   }
   else
   {
      perror ( filename ); 
   }
   
   char wait;
   scanf( "%c", &wait );
   return(0);
}


void search_string(char *file)
{
	char *ptr, *strptr, word[SIZE] = {" "};
	int i = 0, j = 0;

	ptr = file;

	while (*ptr != '\0')
	{
		if (isspace(*ptr))
			break;
		word[i] = file[i];
		ptr++;
		i++;
	}
	strptr = file;
	
	while ((strptr = strstr(strptr, word)) != NULL)
	{
		strptr++;
		j++;
	}

	printf("%s occured %d times in the string\n", word, j);
}

If anyone has any ideas on how to implement my function to meet my needs I'd be very grateful. Thanks

Grn Xtrm 84 Posting Pro in Training

Yes Aia you are right that there are a few dumb mistakes in my code. ;)
I did some searching and found the following code using strstr.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define SIZE 100

void search_string(char *);

int main(void)
{
	char array[SIZE];

	puts("Enter a string:");
	gets(array);
	search_string(array);

   char wait;
   scanf( "%c", &wait );
	return 0;
}

void search_string(char *array)
{
	char *ptr, *strptr, word[SIZE] = {""};
	int i = 0, j = 0;

	ptr = array;

	while (*ptr != '\0')
	{
		if (isspace(*ptr))
			break;
		word[i] = array[i];
		ptr++;
		i++;
	}
	strptr = array;
	
	while ((strptr =strstr(strptr, word)) != NULL)
	{
		strptr++;
		j++;
	}

	puts(array);
	printf("%s occured %d times in the string\n", word, j);
}

I have tried to apply this code to my porblem but have been unsuccessful. Here is what I tried.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000
#define SIZE 100

void search_string(char *file);

int main()
{
    char pattern[] = "of";
    int counter = 0;
    const char filename[] = "in.txt";        
    FILE *file = fopen(filename, "r");    
    if ( file != NULL )
    {
     char line [ MAX ]; 

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
            ++counter;
            printf( "%d  ", counter);
            fputs ( line, stdout ); /* write the line */
      }
      printf( "\n\n");

      search_string(file);
      fclose ( file );
   }
   else
   {
      perror ( filename ); 
   }
   
   char wait;
   scanf( "%c", &wait );
   return(0);
}


void search_string(char *file)
{
	char *ptr, *strptr, …
Grn Xtrm 84 Posting Pro in Training

Have you tried using the following as the condition for the while loop

while (p!=NULL && p!="//")

Then get rid of the if/else and just make the code currently after the else, the text of the while loop. It seems to me this is what you want the loop to do. Please correct me if I'm wrong.

Grn Xtrm 84 Posting Pro in Training

Hello friends, I am having a problem writing a function to search for a string in an input file. I have successfully output the file with line numbers, which was the first step in the program, but now I am stuck on searching for a given string. After searching for the string I want to print the number of times it appears in the input file. As of right now all the function does is return a junk number. I have tried many different things and haven't gotten anything to work correctly.
Here is my code

#include <stdio.h>
#include <string.h>
#define MAX 1000
void find_string(char text[], char pattern[]);
int main()
{
    char pattern[] = "of"; //string to search for
    int counter = 0;
    static const char filename[] = "in.txt";        
    FILE *file = fopen(filename, "r");    
    if ( file != NULL )
    {
     char line [ MAX ]; 

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
            ++counter;
            printf( "%d  ", counter);
            fputs ( line, stdout ); /* write the line */
      }
      printf( "\n\n");

      find_string(filename, pattern);
      fclose ( file );
   }
   else
   {
      perror ( filename ); 
   }
   
   char wait;
   scanf( "%c", &wait );
   return(0);
}

void find_string(char text[], char pattern[])
{
     int matches;
     static const char filename[] = "in.txt";    
     FILE *file = fopen(filename, "r");    
     if ( file != NULL )
     {
        char line [ MAX ]; 

        while ( fgets ( line, sizeof line, file ) != NULL ) 
        { …
Grn Xtrm 84 Posting Pro in Training

I envy you, so what, you just get free pizza? :icon_razz:

Yeah, pretty much. Calzones too. :)
Just as long as there is anything left when we close.

Grn Xtrm 84 Posting Pro in Training

Mmmm. Pizza.

I eat more than my fair share of pizza because I work in a pizzeria. ;)
It really is the best food.

Grn Xtrm 84 Posting Pro in Training

Call the function in main(). Make sure to create 3 variables of appropriate data types to supply as parameters.

int main()
{
double var1, var2;
int var;
sine(var1, var2,  var);
return 0;
}//closes main

You'll need to give these variables values and then output the result of the function call.

Grn Xtrm 84 Posting Pro in Training

Ok your code has a few problems with it. First put this at the top of the code

#include <iostream>
using namespace std;

Next change you variable input to x. This is the variable that will be entered by the user and tested by the if statement.
Next xhange the first if to a while loop and move cin>>x into the while loop.
Then use your current if/else to test the input.
Finally put return 0; before the final right brace }
Your main problem was leaving cin>>x outside of the while loop. You need it in the loop so you can enter many numbers.

Towely commented: You're awesome, thanks! +1
Grn Xtrm 84 Posting Pro in Training

Don't put it after main() put it within main() just before the right brace that terminates main().

Grn Xtrm 84 Posting Pro in Training

You need to put return 0; at the end of main() and remove it from the end of the code. Remove the last 2 lines of the last code you posted.

Grn Xtrm 84 Posting Pro in Training

Post the rest of the code. That seems like it should be working to me.

Grn Xtrm 84 Posting Pro in Training

Can you post the errors being reported. Thanks.

Grn Xtrm 84 Posting Pro in Training

Just look up printing and getting input using cout<< and cin>>. Also, use

#include <iostream>
using namespace std;

That seems to be the only things you really need to change on first glance.
I'm sure William or someone else will correct me if I'm wrong.

Grn Xtrm 84 Posting Pro in Training

Line 127 must be enclosed in parentheses. I'd combine lines 126 and 127 into the following

if(Reply == CorAns)
Grn Xtrm 84 Posting Pro in Training

There are problems with the main().
First there should be no semicolon after int main(void).
Secondly, you must ecnlose the text of the main() with curly braces {}.

Grn Xtrm 84 Posting Pro in Training

Oh , remove the word float from the function calls.
I shoulda spotted that earlier.

Grn Xtrm 84 Posting Pro in Training

Are you getting compile errors or is it just not doing what you want it to do? If you are getting compiler errors, post them here.

Grn Xtrm 84 Posting Pro in Training

Oh, ok. Ty. Sadly I have no clue what a parameter is..I shall look it up though. Thanks again for the information.

A parameter is what goes in the parentheses after a function call. The function call and the function definition must contain the same amount of parameters and they must be of corresponding data types. That explanation may be a little unclear. Look it uo online for more info.

Grn Xtrm 84 Posting Pro in Training

If lines 36 and 44 are supposed to be function calls, you need to put () after the function name. Please correct me if I am wrong, but I think thats the problem.

EDIT
You will need to put in a parameter as well. MAke sure it is the same data type as the one in the function definition.
Sorry I missed that at first glance/

Grn Xtrm 84 Posting Pro in Training

Try searching the forum, this question gets posted all the time. In fact my first post ever was regarding converting infix to postfix using stack. Also, I know there is a code snppet for converting infix to postfix in java. Give it a try.

Grn Xtrm 84 Posting Pro in Training

and last but not least: it appears that all the new improvements (...) caused DW to run 3-4 as slow as it did before (and it wasn't very fast to start with). It's getting worse and worse. I haven't been active in the C++ forum for a week now, because it takes me 15 seconds to load a thread.

I agree. I think maximizing speed should be a top priority. No offense, but Daniweb has been running very slowly recently. And as niek_e pointed out, it does turn people away from reading multiple threads. There's just not enough time in the day to sit through all those long loading screens.

Grn Xtrm 84 Posting Pro in Training

Dumb question...Has this compiler worked before? i.e.have you successfully compiled other programs with this setup.

Yes I have successfully compiled with the current setup. Thats why I am so perplexed right now.

Grn Xtrm 84 Posting Pro in Training

Thanks but I'm not using Linux. I'm using the Dev compiler and have it saved as a .c file.
UPDATE
I wrote a user defined function and I am getting the same undefined reference error when I try to call this function.

Grn Xtrm 84 Posting Pro in Training

Hello friends. I am experiencing problems using the getline function in C. I want to use the function to print line numbers next to the text of each line of an input file. I have successfully read in the file and printed its contents line by line. But when I add the part to print the line numbers using getline, the compiler reports an error and the code doesnt run.
Here is the error I am getting

[Linker error] undefined reference to `getline' 
  ld returned 1 exit status

Here is the code I am using:

#include <stdio.h>
#include <string.h>
# define NEWLINE '\n'
int main()
{
    static const char filename[] = "Bond.in.txt";        
    FILE *file = fopen(filename, "r");    
    if ( file != NULL )
   {
     char line [ 200 ]; 

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
         fputs ( line, stdout ); /* write the line */
      }
/*********** code that gives problem ***********/

int counter = 0;
while ( getline(&filename, &line, stdin) ) 
{
    ++counter;
    printf( "%d , %d", counter, line);	
}

/************* end of code that gives problem****/

      fclose ( file );
   }
   else
   {
      perror ( filename ); 
   }
       char wait;
    scanf( "%c", &wait );
   return(0);
}

I dont understand where this error is coming from. I tried several varaitions using different parameters for getline but all resulted in the aformentioned error. It is my understanding that getline is a built in function included …

Grn Xtrm 84 Posting Pro in Training

It doesnt appear that the variable chunk is declared anywhere. It looks like you should be getting compile errors. Are you? If so, tell us what they are.
EDIT

Nevermind that. I see it is defined as a constant. Sorry

Grn Xtrm 84 Posting Pro in Training

The if statement on line 23 seems problematic to me. There is no integer greater than 999 AND less than 1000.