2

I have written this code where I want to add two integers, two doubles and concatenate two strings out of which one of the integer, double and the string is already declared and the other integer, string and double are to be taken by the user. But it seems that the program isn't taking another string as an input.

I have written a similar program where I can take the string from the user using scanf but the same isn't working here.

int main() {
int i = 4;
double d = 4.0;
char s[] = "My college name is  ";

// Declare second integer, double, and String variables.
int i2,sum1;
double d2,sum2;
char s2[100];

// Read and save an integer, double, and String to your variables.
scanf("%d",&i2);
scanf("%lf",&d2);
scanf("%[^\n]%*c",&s2);

sum1= i+i2;
sum2= d+d2;
strcat(s,s2);

// Print the sum of both integer variables on a new line.
printf("%d\n",sum1);
printf("%.1lf\n",sum2);
printf("%s",s);


return 0;}

After I made the necessary changes like removing & from s2 and changing s[] to s[200], I still cannot get the concatenated string. I am writing my edited code. Kindly help me with that.

int main() {
int i = 4;
double d = 4.0;
char s[200] = "My college name is  ";


// Declare second integer, double, and String variables.
int i2,sum1;
double d2,sum2;
char s2[100];

// Read and save an integer, double, and String to your variables.
scanf("%d",&i2);
scanf("%lf",&d2);
scanf("% [^\n]%*c",s2);

sum1= i+i2;
sum2= d+d2;
strcat(s,s2);

// Print the sum of both integer variables on a new line.
printf("%d\n",sum1);
printf("%.1lf\n",sum2);
printf("%s",s);


return 0;
}

Kindly help me with the bug here.

  • 2
    `strcat` is attempting to write more data to `s` than it can hold. – William Pursell May 13 '19 at 20:23
  • 2
    `strcat(s,s2);` is wrong: `s` has just the size of the string it was initialized to. – Jean-François Fabre May 13 '19 at 20:23
  • 1
    You need to adopt an [indentation style](https://en.wikipedia.org/wiki/Indentation_style) and apply it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it. – tadman May 13 '19 at 20:29
  • @xing I din't get the point that why `scanf("%lf",&d2); ` leaves a newline pending? – Gagan Batra May 13 '19 at 20:47
  • @xing okay sir, i got the point though i am unable to get the concatenated string even after i removed `&` from `s2` and changing `s[]` to `s[200]`. Kindly help me with that. – Gagan Batra May 13 '19 at 20:59
  • @xing Yes sir. Should i show my edited code? – Gagan Batra May 13 '19 at 21:04
  • 2
    The space goes ***before*** the `%`. – user3386109 May 13 '19 at 21:19
  • @user3386109 got it. – Gagan Batra May 13 '19 at 21:25
  • 1
    See also [`scanf()` leaves a newline in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) for the 'skipped data' problem. Note that only 3 `scanf()` conversions don't skip white space — `%c`, `%[…]` (scan sets), and `%n`. Also note that you should check the return value from `scanf()` every time to ensure you got the data you were expecting. – Jonathan Leffler May 13 '19 at 21:52

4 Answers4

2

You are passing the wrong type of argument to scanf. s2 is an array of chars, so &s2 is a pointer to an array of chars, not a pointer to a char.

(You also ought to have bounds checking to prevent array overflows, add a newline to your final printf, etc. But eliminating the & will make your program compile and run)

One Guy Hacking
  • 1,176
  • 11
  • 16
  • I removed "&" in scanf but my output doesn't shows the concatenated result however i am able to type input now. i also changed `char s[] = "My college name is ";` to `char s[200] = "My college name is ";`, but still it isn't working – Gagan Batra May 13 '19 at 20:43
  • @GaganBatra You also need the space character that xing mentioned. `" %[^\n]%*c"` – user3386109 May 13 '19 at 20:45
  • 1
    You have "% [^\n]" now instead of " %[^\n]". The space must come before the %, not after (so you instruct scanf to eat a space, then take everything up to the newline and store it in your variable. You would also be well served to #include and to get the declarations for the functions you use, and to compile with all warnings turned on (from gcc, -Wall -Werror). They wouldn't have helped in this case, but the compiler does find lots of errors for you. – One Guy Hacking May 14 '19 at 00:20
2

It's not taking your string input because you use %[^\n]%*c to scan the string. which instuct the program to return after geting a newline as input. And the string got a newline form the buffer after scanning d2, and return with out taking further input.

To get rid of this you need to input a char before taking the input of the string. Change the following lines:

scanf("%lf",&d2);
scanf("%[^\n]%*c",&s2);

To:

scanf("%lf",&d2);
getchar();
scanf("%[^\n]%*c",&s2);

And your code will take the string input properly.

Additionally, you can also do this (taking a extra character input befor string input) by putting a extra space before % sign.

Changing the following line:

scanf("%[^\n]%*c",&s2);

To:

scanf(" %[^\n]%*c",&s2);

Also do the same thing.

Niloy Rashid
  • 687
  • 4
  • 16
1

Possibly your use of

scanf("%[^\n]%*c",&s2);

As far as I'm aware you can use

scanf("%[^\n]%*c",s2);

or

scanf("%[^\n]%*c",&s2[0]);

As the variable s2 is itself a pointer to the first memory address of the array, using &s2 is just a pointer to a pointer and has no allocated consecutive memory addresses to fill. Hope this helps.

1

Replace:

scanf("%[^\n]%*c",&s2);

With:

fgetc(stdin);   
fgets(s2, 100,stdin);
user11493159
  • 186
  • 1
  • 3