WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I've noticed every time someone claims they have an easy or quick question, you can count on a minimum of a week or two of struggle to get the problem figured out.

If it was so easy, why do you have to ask? :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Try head=&thedata;

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Try here

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You're quite welcome. Odd thing about this bit of birthdays being on the same day - if you have a group of 50 people, the odds are VERY high that two of them will share the same bday.

50% chance with 23 people, 99.999% chance with 38 or more... Very weird.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
else if(i==4){
    strcpy(getdata.p2p_proto, input);
   //printf("%s",getdata.p2p_proto);
   i=0;
   getdata.next=NULL;
 
   return (getdata);
   bzero(&getdata,sizeof(getdata));
}
 
/*else{
 
printf("Wrong File");
break;
}*/
 
//printf("%s",input);
}
fclose(fp);
}

If you get to i=4, where do you close the file? You need to let the loop simply exit and return at the end of the function. And how does bzero() get called?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You cannot load variable values into pointers. You can load addresses into pointers. So get rid of the pointers.

Think about this loop. Run it by hand. Does it do anything useful? Remember which values are variables and which are pointers and either use them properly of change them to what you really need.

while (fscanf(fp,"%d",&ptr )!=EOF)
{
    ptr=a[m];
    m++;
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Really? Show me the declaration for a?
And temp is a pointer, not an integer.

Do not tell us the errors, post them exactly as the compiler displays them.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
palindrome(string[1]);

is passing a single character into the function. You probably want

palindrome(&string[1]);

since that's what the function is expecting.

As for your logic, it's a royal mess. You are expecting the entire string to be available in the function, but never really keep track of the locations you are testing. Based on what I see, you are trying to test each character from the beginning with the last character of the string itself.

To do what you are attempting, you probably need to pass in the string (all of it), 0 (first char position), and strlen(string)-1 (last char position) in the initial call. Then call the itself with string (all of it), incrementing the first position and decrementing the last position.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Its saying that my variables k, a, and temp are undeclared. And there's a syntax error before '}' on line 40. Sorry. I definitely should've put that in my original post.

Yes you should have.

So where did you declare k, a, and temp?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Unfortunately, because there is no formatting at all, few of us are going to take the time to find the equations you're talking about in almost 200 lines. Check this out and repost with a little more detail.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

In this prototype float sum (float (*) (float, int), int*, int s); what are you trying to accomplish with the first parameter float (*) (float, int) ?

Does the code have to be this complex or are you allowed to simplify it?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I am having troubles getting my program to work. It is supposed to take integers from a file and put them into an array. Then it is supposed to sort them by the bubble sort algorithm.

So are we supposed to figure out what kind of problems you're having? Or would you care to tell us? Explain in detail.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yep. http://www.dol.gov/elaws/faq/esa/flsa/002.htm. They're breaking the law.

As I said, research. Learn and understand something out of your sphere of knowledge. There's more out there beyond your world of understanding.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Use some of the programs you had to write when you learned C++.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Assuming the code works that's based on my suggestion, now create a frequency array to count each letter -- freq[26]. Now loop through the array and use the character itself as an index into that array: freq[ar[i]-'a']++ ar is the current character
subtract 'a' from it to offset the value to 0.
++ increments that frequency value.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

My Ghod, you have no idea what you are talking about! Do you honestly think restaurants are breaking the law? Are the people that uphold the law that daft? Sorry, you've just proven my point -- you have no idea what you're talking about and have no idea how the service industry works. Research what you don't understand and learn something new.

Next thing you're going to tell us is that gets() is a good function because after all, K&R wrote it!

:icon_rolleyes:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I think what you have is basically fine for a start. You just have to have some way to exit your input loop -- maybe by entering a grade like -999 as the exit criteria. Keeping count of how many grades were entered makes the output loop easy.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

This is a small project for school but I perfer that it stay standard so it can run on Windows, OSX, and Linux as I on a personal level use Windows AND Linux.

If so, just use getchar() . It's completely standard, All compilers know about it, it's totally portable.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I see the following problems:
1) Using void main() -- see this
2) Your formatting is terrible -- see this. Without good formatting, code is very difficult to follow.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you also strip out line #17 -- you're doing the output in your searchCount() function now -- you should be done.

Actually, it would be better to strip the output out of the function. Let the function do its job (search) and only it's job. Let main() decide when and if the output should be displayed.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

>> how we make the software through c++?
:)
well, I buy or download a compiler, then I make a project & write a program in it. then I compile! ;)

You mean I've been doing it wrong all these years? Bummer! :icon_twisted:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

which is why they're salaried.
If they were not paid a salary, you might have a point, but they are paid already to do exactly that.
If they consider that salary too low, they should try to raise it, not expect me to supplement it through social pressure and "mandatory voluntary tipping".

No, that is why they get a salary under minimum. Because they get tips.

I really wish people would try to understand another industry with different rules rather than try to map their cushy office job into a completely different industry that doesn't work the same. But I guess that's just a pipe dream. Oh, well. Gives me a better insight into the argumentative noobs here that have to do it their way even though the professionals explain what they are missing conceptually.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I think I get it now. Thanks for the detailed explanation.

You are making it way too complex. Your first loop is fine, just lowercase all the letters:

while(str[count]!='\0')     //converting uppercases to lowercase
    {
        if(str[count]>='A' && str[count]<='Z')
        {
            str[count]+=32;  // this magic number is OK

        }
        count++;
    }

Your next loop is way too complex for the job your comment is claiming: /* putting unique characters in ar[] */ First thing is, you don't want unique, you want only letter characters.
Make a loop similar to the first one. You need 2 counters, 1st for the array you read, 2nd for the location in the new array where the next character goes. Start them both at 0

count = 0;
ar_count= 0;

Now when you move the character to the new array, you use something like ar[ar_count]=str[count]; like you did, and increment both counters. But if you don't move the character (it's a SPACE or a QUOTE) only increment count. Don't increment ar_count.

This loop including the initialization above should be about 6 code lines long with 1 and only 1 loop. Remember, this new array is NOT a string, it is simply an array of characters.

Now display the new array and see if it worked.

If you type in "Today this sunny day, IS bright!", your new array should display "todaythissunnydayisbright"


To be continued...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I reallu don't understand this function at all:

static int get_days_in_month(int **days, int year) {
	
	int *array = malloc(sizeof(int) * 5) ;
	days = &array ;

	/* number of days in each month excluding February */
	array[0] = 31 ;
	array[1] = 28 ;
	array[2] = 31 ;
	array[3] = 30 ;
	array[4] = 31 ;
	
	free(array) ;
	
	return **days ;
}

1) Why are you malloc ing and array just to fill it with data, then destroy it with free() ? What's wrong with: int array[] = {31, 28, 31...}; It's always there, ready to do, without the hits on the heap.

2) Why are you passing in a ** and returning a **? If you put the above definition in main() you don't need this worthless function.

3) It will be less confusing and you would not be struggling with this code 2 days later.

4) This function could then be used to calculate the number of days which is where I think you are eventually going.

Again this is a stripped down verion of what your trying to accomplish

and here's my stripped down version:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) 
{
	
    int *days_In_Month = { 0, 31,28,31,30,31,30, 
                              31,31,30,31,30,31} ;	
	
	printf("days in month %d\n", *(days_In_Month + 1));
	printf("days in month %d\n", *(days_In_Month + 2));
	printf("days in month %d\n", *(days_In_Month + 3));
	printf("days in month %d\n", *(days_In_Month + 4));

	return EXIT_SUCCESS;
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you are writing C++ you should be using cout , not printf()

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hi,
Are 2D arrays positioned in memory the same way as normal arrays?
ex.:

char names[2][5] = {"Dean", "Viky"};

If 'names' is memory location 3000, are the letters necessarily stored like this?
...

An easy way to find out is to create the array in main() then pass the array to a function like output(names) . Then output the contents of the pointer one character at a time in hex.

void output(char *p)
{
    int i;
    for (i=0; i<10; i++)
    {
        printf("%2d) %02Xh\n", i, *p); // output the character at the ptr
        p++;   // point at the next character
    }
}

Yes, I used printf() for the output because of the C++ iomanip foolishness -- it makes little sense and this was faster.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

A tip is given for service. Not exceptional service. They bring water, they deserve a tip. They bring your food, they deserve a tip. They do it grumbling and with a frown, they deserve less -- but they still served you.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
if (SAverage <= 100 && SAverage >= 90)

How can SAverage be greater than 100 AND less than 90 at the same time?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Do you know about regular files?

Temporary files are just regular files you use to hold data you need to temporarily save. They aren't special or anything.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Why a linked list? What about just reading all the values into an array? Then you have everything you need right at your program-tips -- so to speak.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Look at the file and the lines displayed. Which lines are displayed?

It looks like you're reading one line, throwing it away, then reading the next line and using it.
This is repeated.
Then you get to the EOF.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And did you make the changes correctly? I can't tell from here.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
for(i = 0; i < g; i++)
       {
             bdays[i] = 1 + rand() % 365;
             for (j = 0; j < g; j++)
            {
                  if (bdays[i] == bdays[j])
                        return 1;
                   else
                       i - 1;
                 if (i == 0)
                       return 0;
             }
       }

This loop makes no sense at all. Your first step is to format the entire code because it's really hard to see what's going on. I reformatted the loop and it still is senseless.

i and j both start at 0. So your IF is always true since bdays[0] will always be equal to itself.
If the IF wasn't true though, you execute i - 1 which does nothing. Then you test if i is zero, which it is, so you return.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

How to copy one 2D-array to another without using loops and library function of C. Not pointers.

No C function? Write it in ForTran, or Algol, or Pascal, or Cobol.
Bliss would be interesting, or Lisp.
It would be hard in RPG-II.
Maybe assembler -- no, there you'd need a loop.

jonsca commented: Programming language "historian" rep +4
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

please help.

Line 44 is

{

What was it before you rewrite it?

i even change it to this but still getting additional numbers.

Changed what to what? Are we supposed to not only find your bug but also find your changes. And in this unformatted code that Salem already pointed out?

Format your code, ask intelligent questions, don't make us guess, and you'll probably get an answer in 1 post, rather than all this lack of understanding on our part and frustration on your part.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

if(tempch>=65 && tempch<=90) tempch+=32; if(str[count]>96 && str[count]<123) What are these magic numbers?

After all that discussion since last night about using the characters themselves ('A', 'z', ' ') you still post code with magic numbers?

Seems AD and J just wasted a night trying to help.

But still it's not working....
here's the new code:

So still the exact same problem? You fixed nothing with all that from last night? Or is this a new problem?

You know, looking back on your posts you love to tell us "it's not working" but you like to keep what's actually happening a secret. Don't keep secrets! Don't tell us it's not working. We figured that's why you're here. What we care about is what exactly it is doing wrong. Exactly. Not a vague "it's broke". A specific "it does this and this instead of that and that". Please be detailed.

Ancient Dragon commented: Yes, I may as well have ignored this thread. +27
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Then write your own atod() . Conversion functions are simple since you have the numbers right there in your string.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

It's called a breadth-first search.
From your starting position (which is the first entry in the solution array), generate and test every possible move in the next free array locations. Keep track of the parent (array[0]).
Move to the next position (array[1]) and generate all moves from that position. Keep this up. You will need a huge array.
If there is a solution, this will find it. But it's fairly slow and memory intense.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

No, no, no. Note the italics. "Just output the string named backwards. You don't have to print the string backwards char by char." In other words, get rid of your last loop. where you used .at is fine.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Just output the string backwards. It's a string. You don't have to print it char by char.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

you don't need a compiler on every computer. Just the .EXE file if it's a console program.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you are trying to learn C++ why are you concerned with making a C program?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Now you created a string array called backwords and loaded each character from word into each string array.

The thing you're missing is 'backwards'. Copy the last character in word to the first location in backwards. Then the next to last to the second. Etc.

Remember, if backwards starts empty, all you need to do is add the character from word with +

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

false and true are not defined in C. You need to define them.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Why are you trying to store the strings "false" and "true" into double variables?

And where did you define Printf()?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

All you are doing is moving 1 character at a time from word into backwards -- but you overwrite backwards each time.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Simple. Read an entire line rather than a single character.
Output the line to the file.
If the first character in the line is \n, you're done. Nothing was entered.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The problem I see is:

dataFile.read(reinterpret_cast<char *>(&coach),sizeof(coach));
	// you just read 1 record

	while(!dataFile.eof())    // Start a loop until you reach end 
	{                         // of file.  Output every character 
	                          // from the start of 'coach' to the 
	                          // end of memory since you never 
	                          // read another thing from the 
	                          // file.  EOF is never seen.
		textFile << coach[i];
		i++;
	}
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Please bare with me. I am still a newb at programming and this may seem a bit confusing, but I have many comments and debug lines. This is an extremely long code, but could the advanced programmers out there please help me out?

You really need to get rid of all the #ifdef's. They make the code very difficult to follow. Remember, you are asking others to read your code, and you don't want it to be difficult to read. Just put the printf()'s in there and comment them out later.

Is this a different program from your other thread. If not, please mark this thread solved and repost where you started. Are you expecting different help in a new thread?


The goal of my program is to simply read an expected number of values, read the values (on one line) themselves, and print their averages. The expected number of values is used as a counter in a while loop. I am trying to make my program robust so that it will print an error when something other than an integer is entered. Therefore, I declare a char string, determine whether each value of string is a digit or letter, and convert the character of that string into an int. My problem with this program is my first while loop. The first loop seems to be fine, but second time around, it is unable to read user input. It instead uses the last output …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I replaced my scanf with an fgets and this is what I got:

--snipped---

It compiled fine, but my debug statements were different compared to my code with a scanf. The results were not correct. Did I code my fgets incorrectly or am I better off with scanf? My assignment is almost due and I am so frustrated... could anyone please help me?

For the second scanf, I plan to change it to read a string once I get the first one to work.

I'm not psychic. I don't know what's different, don't know what's not correct, nor how. You have to explain since you are the only one that knows.

No, you are not better off with scanf()