0

I am supposed to create two arrays of char called input1 and input2 that can hold 1000 characters each and one array of char called input that can hold 2000 characters. Use the gets function to read in two different sentences from the user and store it in the variable input1 and input2. Join input1 and input2 and place the result in the input variable.

here is my code

    main() {
     char input;
     get(input1);
     get(input2);
     input = input1 + input2;

    }

    char a(string result){ 
     char input1[1000][1000];
     printf("please enter the something\n"); 
     scanf("%s", input1); 
    return input1; 
    }

    char b(string result){ 
     char input2[1000][1000];
     printf("please enter the something\n"); 
     scanf("%s", input2); 
     return input2; 
    }
aPEARR
  • 53
  • 1
  • 10
  • 1
    You have a lot of problems with your code and your understanding of the language and the problem. However, that aside - what error are you getting? Because as it stands, your question isn't a question per se - but rather an open invitation for someone to do it for you. – jrd1 Jul 20 '13 at 17:25
  • @jrd1 please stop condemning me, I understand that i am a beginner in c programming. I am trying to piece the stuffs that i know about c together into the code. Do a have a website that allows me to learn c in a better way? And lastly i am not asking for a open invitation for someone to do the code for me, please get it right, i am asking for help here so i understand it and be able to do it in future. I know there is something wrong with my code, but i do not know where have i gone wrong. – aPEARR Jul 20 '13 at 17:37
  • I have not condemned you. We're all here to help each other, bro/sis. I am simply stating and connecting the facts lain before me. **In that, you have only posed a problem, an attempted solution, but no question per se.** Second, the solution you've posted is incongruent with the usage of the language as it pertains to understanding that problem wants you address. There is nothing wrong with that - we're all novices at some point. – jrd1 Jul 20 '13 at 18:01
  • However, since your solution is flawed, and _most importantly_, **there is no apparent question relative to the problem with your code**, by simply **not asking a question** - _you've simply asking for an answer_ - hence my statement. The response you posted should have been in the question, IMO. If it had been, my response would be directed to the error you didn't include. Something to note in future. – jrd1 Jul 20 '13 at 18:02
  • Regarding sites and books, look at this post: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list Good luck! – jrd1 Jul 20 '13 at 18:03
  • @jrd1 sorry, i am new to this site, new to c programming. btw whats se? i just dont know why people out here sometimes like to condemn me. in a way, i am a novice in c programming, i am giving of all i know to the code after researching. maybe its just the way i phrase my sentence? I also mean, i tried and tried to do the questions and research about it, but the feedback i receive is always negative. – aPEARR Jul 20 '13 at 18:18
  • `se` is part of the Latin phrase [per se](http://en.wiktionary.org/wiki/per_se), which in this context means "by itself". How you phrase your question might be part of the problem, but IMO, I think the problem is more primal than that. One, you're a new user to the site, and it takes some getting used to all the stuff that's required. Secondly, and most important, whether you know it or not, you've actually joined a community, which by definition means you also are part of that culture. And, like every society, you have to adapt in order to thrive in that society, which applies here. – jrd1 Jul 20 '13 at 18:45
  • So, what that means, is go over the StackOverflow faq. Begin poring over the http://meta.stackoverflow.com site, try your hand at answering questions on here if you can, and try to adapt your questions to conform the preamble outline in the [faq](http://stackoverflow.com/help). It's tedious at first, but eventually it gets easier. Also, I can't speak for others, but it's a pet peeve of mine that people use proper punctuation and grammar _where possible_ as often as possible. – jrd1 Jul 20 '13 at 18:51
  • Why? It's psychological in that everyone here is communicating without seeing each other, but subconsciously weighs them based on adherence to certain rules or preferences. What that means is that people generally like people who are like them and think like them. IMO, since programmers work with strict rules, that adherence and devotion to other things is even stronger. People will be more likely to help you if you conform to the rules (intrinsic or implicit). Just an observation. :) – jrd1 Jul 20 '13 at 18:52
  • Your title and code refer to a `get` function, which does not exist in standard C. Your question refers to `gets`, which does exist, but should never be used because it's inherently unsafe; in fact it was removed from the language by the 2011 ISO C standard. If your assignment specifically says to use `gets`, I suppose you should go ahead and use it, but be aware that it should never be used in production code. `fgets` is similar and can be used safely, though it's a little more complicated. – Keith Thompson Jul 20 '13 at 19:03
  • 1
    You might find the [comp.lang.c FAQ](http://www.c-faq.com/) useful, though it's primarily intended to answer questions after you've already learned much of the language. – Keith Thompson Jul 20 '13 at 19:04
  • @jrd1 Sorry what's IMO? – aPEARR Jul 20 '13 at 19:17
  • @KeithThompson I tried google-ing for fgets but there I do not really understand, could you explain them? – aPEARR Jul 20 '13 at 19:25
  • @PEARR: IMO: In my opinion. `fgets` is similar to `gets`, except that (a) `gets` reads only from stdin, `fgets` takes a `FILE*` argument; (b) `fgets` leaves the `'\n'` in the string, (c) `fgets` takes an argument that specifies the size of the target array, which means it can be used safely. – Keith Thompson Jul 20 '13 at 21:39

2 Answers2

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

int main(){
    char input1[1000] = {0};
    char input2[1000] = {0}; 
    printf("please enter the something\n"); 
    scanf("%999[^\n]%*c", input1); 
    printf("please enter the something\n"); 
    scanf("%999[^\n]%*c", input2); 
    char input[2000]; 
    sprintf(input, "%s %s", input1, input2); 
    printf("%s\n", input);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • sorry what does %999[^\n]%*c means? – aPEARR Jul 20 '13 at 19:33
  • 2
    @PEARR The `999` means that `scanf` will read at most 999 characters. The `[^\n]` means that it should read anything except newlines. Check e.g. [this reference](http://en.cppreference.com/w/c/io/fscanf). – Some programmer dude Jul 20 '13 at 22:58
  • @PEARR Mr. Pileborg is tell you already. `999` is the maximum width of the number of characters to read. You draw one character for EOS('\0'). `[character set]` to specify the character type to read, this(`[^\n]`) is to indicate that you read other than the newline. `%s` is, using a method like this because it separates the space(white) character. `%*` To indicate that you ignore the elements of one. `%*c` is assumed that you ignore the new line following the input. – BLUEPIXY Jul 21 '13 at 10:22
1

To start with, in C there is no type called string.

To continue, with the declaration

char input1[1000][1000];

you declare input1 to be an array of arrays, or an array of 1000 strings where each string is 999 characters long.

You are also trying to return this array of strings from functions returning only a single character. Even if you change the functions to have only a single string, and you return a pointer to this string, you will still have problems (you can't return pointers to local variables).

Then you can't use the + operator to concatenate strings.

All in all, you need to start over with a good tutorial, and get a better grasp of the language first.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621