Aia 1,977 Nearly a Posting Maven

>but I'm a bit confused in understanding why it works like that. Why is only one of the variables, x or y, needed for it to work?

That's the way you designed. Functions can only return one value. And you are calling that function once for X and another time for Y in main.

>Also, how would I make it read decimal numbers, round it, and accept it? For example, 123.123 is entered, so 123 suppose to be read. I'm also trying to find out how to do this.

Read on.

Aia 1,977 Nearly a Posting Maven

>When I input zero for X, it moves on and ask for Y. Now, it's not suppose to do that. It's suppose to output an error message and asking me for a different input.

The get_input () has a logic flaw then.
It asks for X and then for Y, when it should ask only for either X or Y.

Edit:

Continuous loop:
     Display question
         if input entered
             is x more than 0
                 return x
             x is not more than 0
                 Display error and it will loop again
Aia 1,977 Nearly a Posting Maven

Yeah, the above code will take in whitespaces as well. Or you could use gets() or fgets() to be safe.Reason not to use gets(courtesy: Salem :))

If you know what's wrong with gets() why do you say "Or you could use gets()". Why bring it up? Forget that exists. Period.

And yet, scanf can be clever function:

Nonetheless, it would not be very clever not to check the return of it.

Salem commented: *nods* +23
devnar commented: Just saying it's possible, although not safe. +1
Aia 1,977 Nearly a Posting Maven

A for loop could work. Any loop will do as long as you can control when scanf() will not read anything more from the input stream, and also will assign a char into every subscript of string except the last one which needs to be a '\0' to convert the whole thing into a string.

Be mindful that the user can enter one char or none (just the enter key), also it can input more that the string can hold, going over.

Aia 1,977 Nearly a Posting Maven

ok i have changed it to this, but m gettin a warning "comparison between pointer and integer"

int getInputByscanf(char input[]){
     char string[4];
     int result;
	 result = scanf("%c", &string);
		while (string != EOF)
			string = scanf("%c", &string);
		
     System("Pause");
	 return 0;    
}

result = scanf("%c", &string); string is an array of four. scanf() needs only one char. result = scanf("%c", &string[0]); will read one character into string[0] and return 1 into result.

[edit:] can you figure a loop so scanf() can use every subscript of string?

Aia 1,977 Nearly a Posting Maven

is this right??

No.
scanf() returns an int, representing how many formats conversion was able to fulfill.
You use an array of chars string = scanf("%c", &phrase); Therefore string needs to be change to int, furthermore, every call to scanf will bring a new return. int result; result = scanf("%c", &phrase); will result in 1 if it was successful. result = scanf("%c%c%c", &char1, &char2, &char3); will result in 3 if it was successful.

Aia 1,977 Nearly a Posting Maven

>No it will not. It will return zero in that case.
Correct. EOF would be returned ONLY if there's input failure before any data could be successfully read.

>If there is one value to be read, and none are, the return value is zero.
In this case when the user enters the wrong input a mismatch occurs and zero is returned.

Aia 1,977 Nearly a Posting Maven

The only reason this code should give you error is if you input something else that float, let's say letter [...]

scanf() would return an EOF in that case.

Aia 1,977 Nearly a Posting Maven

If that is the actual code, it shouldn't give you any error.

Aia 1,977 Nearly a Posting Maven
Aia 1,977 Nearly a Posting Maven

But I don't want the last set to end with a (,). Could you give me code without the last printed (,)? Pleazzzzzzzzzzz!

Well, srivtsan, finally your hard work of asking here, here, here and over here has payed off. ;)

Aia 1,977 Nearly a Posting Maven

Here are the 2 errors- error C2296: '+' : illegal, left operand has type 'int (__cdecl *)(int)' and cpp(27) : error C2082: redefinition of formal parameter 'i'

You're still compiling your C code as C++ code.

Aia 1,977 Nearly a Posting Maven

Hello,

Thank you for all your help someone brought to my attention that I need to write a function sum that will be called by main().

How can I do that with the code I have done already

Do yourself a favor. Don't cheat yourself out of the opportunity of learning about functions. It is a vital part of creating good programs. Take the time to learn it well. Practice, practice, practice, if you are serious about learning.
Here's a basic tutorial. Use your favorite search engine for more. Post examples if you don't understand. Don't ask anyone to do it for you.

Aia 1,977 Nearly a Posting Maven

Inside the for loop substitute the if for a do/while.

...
do {
        printf("Enter salary #%i: ",i);
        fflush(stdout);

        if (!(scanf("%d", &i) == 1)) {
            fprintf(stderr, "Error, you didn't enter a valid input");
            break;
        }
    }while( i < 0);
...

Thank you for taking the time of reading and using code tags. If you add a c to , next time it makes it specific to the C language.[code=c], next time it makes it specific to the C language.

Aia 1,977 Nearly a Posting Maven

If what you want is to just increment that n by three several times then: n = n + 3; through the loop. If initially sum = n; then sum = sum + 3 is a possibility. But I don't think n becoming the flag that would stop that for loop is what you want.
In any case, for(i=0; i<=n; i++); makes the loop do nothing meaningful.

Aia 1,977 Nearly a Posting Maven

If you are looking to convert strings and into integers, you may want to check out the atoi() function.
Chris

Not my first or second choice. atoi() falls short at error handling.
strtol() is a better alternative.

Aia 1,977 Nearly a Posting Maven

I don't see any increments of three anywhere.
Shouldn't sum be initialized to zero?

Aia 1,977 Nearly a Posting Maven

The last call to printf(), it is being made before variable c has anything meaningful assigned to it.

That while() loop doesn't have a way of stopping. Of course, it would not even get there if a is greater than b.

[Edit:] Why do you think you need to #include "stdafx.h"?; you are not using variable result for anything.

Aia 1,977 Nearly a Posting Maven

>cpp(23) : error C3861: 'mypower': identifier not found
Do you know what that cpp means?
You are compiling the code as a C++ program.
C code must end as *.c

Anyway. The error says that you are not prototyping the function mypower correctly.

Aia 1,977 Nearly a Posting Maven

There is a difference between a character array and a string. A character array char a[3] = {'a', 'b', 'c'}; is not a string. Just because it happens to end in a '\0' when you don't define all the elements char a[3] = {'a', 'b'}; does not make it a string. Don't use it as such.

I admit that I don't know if you are talking semantic definitions over here, but I disagree. What it makes an string a string is that '\0', and as long as the automatic assignment is honored by the compiler, that is for all intend and purposes a string. Whether the '\0' falls in the last subscript or before that.

Is it a lousy way of making a string? Sure. But not more than assigning the elements individually as a[0] = 'a'; a[1] = 'b'; a[2] = '\0'; So, please, explain "There is a difference between a character array and a string."

Aia 1,977 Nearly a Posting Maven

By char a[3]={'a','b'}; you are implicitly assigning a zero to a[2] which is what makes the array a string.

>So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?
Because you can't do this printf("%s",a); if variable a were an array of floats or ints.

Aia 1,977 Nearly a Posting Maven

nevermind i figured it out

Great, glad to hear it. At bottom of the page mark post as solved.

Aia 1,977 Nearly a Posting Maven

Ahh, dude.. I wrote it in java for you:

http://schulte.homelinux.com/ftp/pascal.java

Nice avatar btw, aia..

Josh that doesn't help much in this forum.
And Pascal triangles are represented as an Equilateral or Isosceles where the angles are equal at the bottom, not as a right angle as your program displays.

By the way, if you want to get rid of the warning of your program.

javac -Xlint pascal.java
pascal.java:33: warning: [unchecked] unchecked call to add(E) as a member of the raw type
java.util.ArrayList
                        rows.add(newRow);
                                ^ 
1 warning

After main declare ArrayList <int[]> rows;

Aia 1,977 Nearly a Posting Maven

better?

Not quite. Make use of the C in the (code=c). Example
Of course, that would not be worth a pinch of dirt if you did not care about proper indention before hand.
After that take a look at void main()
Also, unless you have copied the standard header file stdio.h to the current directory, substitute "stdio.h" for <stdio.h>

That's how much your effort of using tags buys from me, until you show some more.

Aia 1,977 Nearly a Posting Maven

A comment if I may bhoot_jb.

another situation is obvious..which..i think you are facing..

You make heavy use of [..]. I know what ellipsis are [...], but I can't figure what's the process that warrant the use of two consecutive periods.

gud luck..

What's the advantage of misspelling "good" to just save the writing of one more vowel?
Why not "gu luc"? That seems to me more advantageous. Better yet, "guluc" will save even the space.
Of course, all is for naught because of the mysterious two periods afterward.

Aia 1,977 Nearly a Posting Maven

if the function returns an int what do you think the variable that stores that value should be in main?
Either terminate the literal string in printf with a \n or make a call to fflush(stdout) after.
Otherwise there's not guarantee that it will display when you want.
A call to exit() requires the inclusion of the standard header file stdlib.h

Aia 1,977 Nearly a Posting Maven

If you don't want to exit don't use the function exit() which will terminate the program.

sentinel set to not done
while not done:
      menu options:
      switch:
      option 1:
           do this and break
      option 2:
           do this and break
      option 3:
          I am done set sentinel to done.
continue flow of program
Aia 1,977 Nearly a Posting Maven

Perhaps consider a menu using a switch case construction?

Aia 1,977 Nearly a Posting Maven

Handling command line arguments
Maybe that would help?

Aia 1,977 Nearly a Posting Maven

Either you need to get more RAM for your head, or you are ignoring on purpose what you have been told before.

Why don't you post that in C++ since that's what you keep compiling your program with?

Aia 1,977 Nearly a Posting Maven

> swap(&a[x][y], &a[++x][y]); pre-incrementing here is undefined behavior. Explanation can be found here.

devnar commented: Solved the prob in a jiffy :) +1
Salem commented: Indeed it is so +21
Aia 1,977 Nearly a Posting Maven

Assuming you don't use pointers yet:
Change the definition of your CountVowels, and prototype it accordingly. counter_t CountVowels(char text[], counter_t Count) Then make the correct call to it in main. Count = CountVowesl(text, Count);

Aia 1,977 Nearly a Posting Maven

for (i = 0; text[i] != '\0', i++) Change to a (;) gets(text); Very, very bad idea. Forget there's such a function. Not safe. fgets(text, sizeof text, stdin); is what you want. fflush (stdin); fflush() accepts only an output stream; anything else is undefined behavior.

int CountVowels (char text[], counter_t Count)

If CountVowel returns an int, you can not try to force it to return a structure of type counter_t like: return (Count); counter_t CountVowel(char text[], counter_t Count) could be a possibility

Next time use code tags to present your code. Read here

Aia 1,977 Nearly a Posting Maven

Some to consider,

int HIGH=16; /*(Max value of an array element)+1*/

That could be instead a constant

#define HIGH 16

Now you can use HIGH as the subscript size of done,

int done[HIGH];

All that,

time_t secs;
time(&secs); /*assign 'secs' with the current time*/

srand((unsigned)secs); /*initialize the seed for rand()*/

can be substituted by, srand((unsigned) time(NULL)); All these,

for(i=0;i<15;i++)

done[i]=0; /* None of the 16 numbers have been used yet*/

could have been done by, done[HIGH] = { 0 };

for(i=0;i<4;)
  {
    .
    .
    .
     i++;
  }

There's not advantage in taking that incrementation out of the for loop and it is kind of ugly. val=rand()%HIGH; /*generates a number between 0 and 15*/ You are randomizing the order of 16 numbers, not assigning 16 random numbers.

Aia 1,977 Nearly a Posting Maven

for(int i = 0; i <= 9; i++ ) This should give you a warning unless you are using C99.
Proper way:

int i;
for (i = 0; i <= 9; i++)
for(int i = 0; i <= 9; i++ )
   {
      if(charstring[i]=='0')
         intstring[i]=0;
      if(charstring[i]=='1')
         intstring[i]=1;
      if(charstring[i]=='2')
         intstring[i]=2;
      if(charstring[i]=='3')
.
.
.
.

Think for a moment. If charstring is a '0', why does the function need to keep looking for other values all the way down?
You are missing some elses.

if (charstring[i] == '0')
    /* statement here */
else if(charstring[i] == '1')
    /* statement here */
else if(...)
     .
.
.
.

However there's a keyword in C named switch which will simplify the condition.

>it does not give me any errors, but when main tries to read intstring it only prints d's as if the function did not change anything.

Are you doing in main, something similar to:

char charstring[] = "0123456789";
    int intstring[10] = {0};
    int i;

    convertString(charstring, intstring);
    for (i = 0; i <= 9; i++)
        printf("%d\n", intstring[i]);
Aia 1,977 Nearly a Posting Maven

Here is an example in C language:

// spell a text string forward and backward

#include <stdio.h>

int main(void)
{
  char text[80];
  int k;

  strcpy(text, "Spell this string forward then backward");
  printf("String = %s \n", text);

  // forward spelling puts a space between characters
  for(k = 0; k < strlen(text); k++)
    printf("%c ", text[k]);
  printf("\n\n");

  // now the reverse spelling
  for(k = strlen(text)-1; k >= 0; k--)
    printf("%c ", text[k]);
    
  getchar();  // wait
}

The standard C functions strlen() and strcpy() require the inclusion of the header file string.h. Furthermore there was not need to add the extra code: strcpy(text, "Spell this string forward then backward"); C is capable of doing that for you as: char text[] = "Spell this string forward then backward"; No need to declared as an array of 80.
Calling strlen(text) every time inside the loop is extra too. size_t len = strlen(text); before the loop is all that you need, then you use len in the first for loop, and len -1 in the reverse for loop. int main(void) { /* requires a return 0; on success */ }

Aia 1,977 Nearly a Posting Maven

[...]
If they don't use the code tags, they don't use the code tags. Ignore it. The polar ice cap won't melt because they forgot the code tags.

Ignoring is going to make it worse.
There's a reason why some forms of conduct are not allow in a cinema. i.e. you are not allow to talk constantly in the middle of a movie. What would happen if that simple rule of behavior would not be enforced in such establishment?
No movie goer would step inside that theater.
Likewise there's reason to maintain some code posting rules. No many, but some, almost common sense, easy to follow. Otherwise, posting will degenerate to whatever the visitor set as standard.
I don't bring any solution to this conversation, other than preventing any post to be answered until correction to it has been made by OP. Perhaps, some automatic generated warning post follow-up, pointing to the proper way of posting code.

Aia 1,977 Nearly a Posting Maven

if i=1 and d=1 would d<i execute? What do you say?

Would if(t == i) have a chance of being execute without t being initialized to any value?

Aia 1,977 Nearly a Posting Maven
/* replaced 0 by 1 */

Depending on who you ask, the first fibonacci number can be 0 or 1. (Personally, I'd agree with you and have it 1.)

Ah, now I know what I did wrong. I did not consult with who.
Don't read too much into it. ;)

Why not use more descriptive names than num1, num2, etc?

I was afraid that it would turn into something like this:

int fibonacci(int num)
{
  return (num < 2 ? num : fibonacci(num-1) + fibonacci(num-2));
}

And then I would not been able to use all those nice green /*comments */ . I like green.
Again, don't read too much into it.

Aia 1,977 Nearly a Posting Maven

[...] i tryied everything

No everything.

int Fibonacci(int fnum)
{
	int num1, num2, num3, fib; /* deleted n1, not needed */
	num1 = 1; /* replaced 0 by 1 */
	num2 = 1;
        /* changes in the loop: */
        /*  fig = 3 instead of fib=1 */
        /*  fig <= fnum instead of fib=num2 + 1 */
	for (fib = 3; fib <= fnum; fib++)
	{
		num3 = num2; /* num2 instead of num1 */
		num2 = num1 + num2; /* lvalue num2 instead of num1 */
		num1 = num3; /* lvalue num1 instead of num2 */
                
               /* byebye if/else statements */
        }
		
	return(num2); /* return num2 instead of num1 */
}
Aia 1,977 Nearly a Posting Maven

>Does that cause a problem???
There can not be two functions named with the same identifier. Even if they both do the same.

Aia 1,977 Nearly a Posting Maven

Name putw() to something different like putword(). Most likely there's an implementation of putw() already prototyped in your compiler library.

Please, make sure you use proper tags when you post code next time. Here's how.

Aia 1,977 Nearly a Posting Maven

[...] What else is this database used for besides conducting 6 degrees of separation studies?

Main purpose is to learn how to better tailor targeted ads to you and yours. Now the possibilities for other uses are limitless, or I should say; limited only by their imagination. I leave to you to speculate who "they" are.

Aia 1,977 Nearly a Posting Maven

I think the new Pope is quite hip! Let's support the poor fellow with your tax deductible donation!

In Spain the working people are obligated by the state to contributed to the Roman Catholic Church whether they want or not. This is accomplished via income tax, but is not deductible. In fact, many even don't know that some of their money goes to the archives of the Catholic Church.

Aia 1,977 Nearly a Posting Maven

You are loosing precision. For percentages you need to use floating points.

Aia 1,977 Nearly a Posting Maven

In main:

int result;

result = GCD( n1, n2);
printf("result = %d\n", result);
Aia 1,977 Nearly a Posting Maven

Regardless of any other logic flaws, I would like to point to some of them.

void process_input(FEELING *ex_1)
{
    char line[200];
    char *pstr;
    int count=0;

    fgets(line,201,stdin); /* line can only hold a buffer of 200, there's opportunity for overflow because of that 201 */

    ex_1->num = -1;

    pstr = strtok(line," \n\r");
    while(pstr != NULL)
    {
        if(ex_1->num == -1) /* never has any opportunity of be something else */
            ex_1->emotion = pstr; /* Keeps overwriting the variable to pointing to different tokens every time through the loop */
        else
            ex_1->emotion_options[ex_1->num] = pstr; /* never happens */

        pstr = strtok(NULL," \n\r");
    }
}
Aia 1,977 Nearly a Posting Maven

You would have to post your modified code, to answer that last question.

Aia 1,977 Nearly a Posting Maven

If fread worked for 1st and 2nd time, then surely the file was opened.

Regardless, I was giving you a suggestion towards good code practice.

Maybe the problem is in wich mode i open the file? like "wr+"

fread() and fwrite() requires you to open the file in binary mode, appending a `b' to it.
i.e. "r" would be "rb"

Salem commented: Make the rep count +17
Aia 1,977 Nearly a Posting Maven

How's t and t2 declared? FILE *fp=fopen("element.txt","rw+"); How about checking that the file was opened?

if ( fp == NULL )
{
     <error...>
}

Upon success fread() returns the amount of elements read in a form of size_t object; why don't you check that?

size_t result = 0;
result = fread(&t, sizeof(int), 1, fp);
if( result > 0 )
{
     <do something...>
}