0

I am trying to get both parts of this program to work as you can see I have split it with first part (question 1) and second part (question 2). The problem is first part runs fine just when the second part starts I can not input any string and it just seems to skip through the code without letting me input the string.

If I delete the first part (question 1) of the program then everything works fine and I can input the string. What interrance is causing this issue.

int main()
    {
        first();
        second();

    }

//Question 1

int first()
{
/* dataarray.c */
float data[20] = {
  50.972438, 93.765053, 9.252207, 1.851414, 16.717533,
  71.583113, 97.377304, 20.352015, 56.309875, 0.072826,
  23.986237, 36.685959, 80.911919, 86.621851, 53.453706,
  96.443735, 29.845786, 18.119300, 31.079443, 52.197715 };

/* The number of elements in the data array */
int data_size = 20;
int pos;
int j;
int i;
int k;

    printf("Question 1\n");

for(i=0;i<data_size;i++)
{
    printf("\nArray %i is %f ",i,data[i]);    //Initial arrays print statement
}
    printf("\n\nArray number to delete:");   //User Choose one to delete
    scanf("%i",&pos);

k =0;
for(j = 0; j< pos;j++) 
{
    printf("\n Array %i is now %f ",k,data[j]); 
    k++;
}

k=pos;
for(j=pos+1;j<data_size;j++) 
{
    printf("\n Array %i is now to %f ",k,data[j]); //Shows array changed to
    k++;
}
data_size = data_size - 1;  //Decreases data size
}

//Question 2

int second()
{
    printf("\n\nQuestion 2\n");

int a,b,check=0;
char str[20];
     printf("\nEnter a String:\n"); //User inputs word to check if its palindrome
gets(str);

for(b=0;str[b]!=0;b++); //Starts at 0 increment till the last length
b=b-1;
a=0;
while(a<=b)
{
    if(str[a]==str[b]) //String a is forwards b is backwards
    {
        a=a+1;
        b=b-1;
        check=1; //If check = 1 then a palindrome
    }
    else
    {
        check=0; //If check = 0 then it not a plaindrome
        break; //Loop break
    }

}

if(check==1)
    printf("It is a Palindrome:"); //Statement printed if check = 1
else
    printf("It is not a Palindrome\n"); // Else if 0 this statement is printed

}
2
  • I think you'll progress fastest if you turn your compiler warnings all the way up, and work on getting a clean compile. Commented Mar 26, 2012 at 18:16
  • And I just noticed gets(). Never, ever use gets(). Commented Mar 26, 2012 at 18:27

1 Answer 1

1

When you call scanf in part one, I presume you enter a number followed by a newline. scanf consumes the number, but the newline is left in the buffer. The gets() in part 2 then sees the newline and returns a blank line. An easy solution is to do

scanf( "%i\n", &pos );

BTW, never use gets. Use fgets instead.

Sign up to request clarification or add additional context in comments.

6 Comments

I didnt understand sorry, im new to c. Care to explain in more detail.
@Amandeep Does adding \n in the scanf format string solve the problem for you? Without it, scanf only consumes the digits that were typed, leaving the newline in the buffer. Then, when you call gets, the newline is consumed and gets returns with a blank line in the buffer. If you add '\n', then the trailing newline is consumed when the integer is read, so gets blocks until more input is entered. Also, it cannot be stated often enough--never use gets!
yes it did cause a problem, but the way the code is for me now question 1 runs fine. Its question 2 that is the problem in question 2 it doesnt allow me to enter a string. I tried the way you have said but that doesnt help actully makes it worser.
anyone who can help im really stuck maybe if i send my code someone can check itim really stuck and need to get this done..
placing it the way you have recommened leaves a blank in the output, then a blank line appears. until another number is written then it responds.But it still doesnt answer my question as in question two i cannot enter a string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.