![]() |
| | #1 |
| Registered User Join Date: Jul 2004
Posts: 46
| Reading strings input by the user... I have been stuck at this for a few day's now , I need too write a program that will use scanf() to read in seven strings input by the user. The program should print the seven strings as a list , then sort them alphabettically and print a new list. I need to use the function strcmp() , & the preprocessing directive #define N_STRINGS 7 to assist in the sorting of the strings I have tried using for loops & while loops with scanf() in reading in the strings , & then printing it out but im all ready stuck At this point im just trying to get my code to read 7 strings input by the user & just print them out again. This is what i've come up with sofar but it's not working at all & very long . Code:
#include <stdio.h>
#include <string.h>
#define N_STRINGS 7
#define SIZE 100
#define SIZED 700
void main(int argc)
{
char c;
int i;
int count = 0;
char array1[SIZE];
char array2[SIZE];
char array3[SIZE];
char array4[SIZE];
char array5[SIZE];
char array6[SIZE];
char array7[SIZE];
printf("Type in 7 strings...\n\n");
if (argc < SIZED){
while ((c = getchar()) != '\n'){
scanf("%s", array1);
count++;
}
array1[count - 1] = '\0';
count = 0;
while ((c = getchar()) != '\n'){
scanf("%s", array2);
count++;
}
array2[count - 1] = '\0';
count = 0;
while ((c = getchar()) != '\n'){
scanf("%s", array3);
count++;
}
array3[count - 1] = '\0';
count = 0;
while ((c = getchar()) != '\n'){
scanf("%s", array4);
count++;
}
array4[count - 1] = '\0';
count = 0;
while ((c = getchar()) != '\n'){
scanf("%s", array5);
count++;
}
array5[count - 1] = '\0';
count = 0;
while ((c = getchar()) != '\n'){
scanf("%s", array6);
count++;
}
array6[count - 1] = '\0';
count = 0;
while ((c = getchar()) != '\n'){
scanf("%s", array7);
count++;
}
array7[count - 1] = '\0';
}
else {
printf("Type in 7 strings...\n");
}
printf("\n");
printf("Printing the strings again ...\n");
for (i = 0;(c = getchar()) != '\0';++i){
printf("%s",array1[i]);
}
for (i = 0;(c = getchar()) != '\0';++i){
printf("%s",array2[i]);
}
for (i = 0;(c = getchar()) != '\0';++i){
printf("%s",array3[i]);
}
for (i = 0;(c = getchar()) != '\0';++i){
printf("%s",array4[i]);
}
for (i = 0;(c = getchar()) != '\0';++i){
printf("%s",array5[i]);
}
for (i = 0;(c = getchar()) != '\0';++i){
printf("%s",array6[i]);
}
for (i = 0;(c = getchar()) != '\0';++i){
printf("%s",array7[i]);
}
}
Thanks a lot for any help Btw I must use scanf() in reading in the strings it says so in my textbook . |
| Cmuppet is offline | |
| | #2 |
| Compulsive Liar Join Date: Jul 2004
Posts: 149
| >void main(int argc) This is way wrong. First, main returns int. Second, the only two portable definitions take 0 and 2 arguments. Third, you aren't using argc correctly. ![]() It's better to leave the implementation to the nitty gritty details at this stage. Study the following and incorporate what you learn into your own code. You'll probably need to replace qsort with your own custom sorting routine though. Most teachers don't like you to use it. ![]() Code: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N_STRINGS 7
int cmp(const void *a, const void *b);
int main(void)
{
/* Declare an array of strings */
char strings[N_STRINGS][BUFSIZ];
int i;
/* Read N_STRINGS strings */
for (i = 0; i < N_STRINGS; i++) {
/* A prompt is always helpful to users */
printf("Enter string %%%d: ", i + 1);
/* Unless you print a '\n' in the prompt, you need this */
fflush(stdout);
/* ALWAYS check scanf for success */
if (scanf("%s", strings[i]) != 1) {
fprintf(stderr, "Error reading input\n");
return EXIT_FAILURE;
}
}
/* Sort the array using cmp as the comparison routine */
qsort(strings, N_STRINGS, sizeof strings[0], cmp);
/* Print out the sorted result */
for (i = 0; i < N_STRINGS; i++) {
printf("%s\n", strings[i]);
}
return EXIT_SUCCESS;
}
int cmp(const void *a, const void *b)
{
return strcmp((const char *)a, (const char *)b);
}
|
| Robc is offline | |
| | #3 | |
| Helper Join Date: Jun 2004
Posts: 255
| Quote:
Code: Compilateur: Default compiler Building Makefile: "D:\Dev-Cpp\Makefile.win" Exécution de make... make.exe -f "D:\Dev-Cpp\Makefile.win" all gcc.exe -D__DEBUG__ -c main.c -o main.o -I"C:/DEV-CPP/include" -W -Wall -O2 -g3 main.c:8: warning: return type of `main' is not `int' main.c:8: warning: `main' takes only zero or two arguments main.c: In function `main': main.c:70: warning: format argument is not a pointer (arg 2) main.c:73: warning: format argument is not a pointer (arg 2) main.c:76: warning: format argument is not a pointer (arg 2) main.c:79: warning: format argument is not a pointer (arg 2) main.c:82: warning: format argument is not a pointer (arg 2) main.c:85: warning: format argument is not a pointer (arg 2) main.c:88: warning: format argument is not a pointer (arg 2) gcc.exe -D__DEBUG__ main.o -o "Projet1.exe" -L"C:/DEV-CPP/lib" C:/Dev-Cpp/lib/libws2_32.a -g3 Exécution terminée Compilation OK
__________________ Emmanuel Delahaye "C is a sharp tool" | |
| Emmanuel Delaha is offline | |
| | #4 |
| Registered User Join Date: Jul 2004
Posts: 46
| Thanks for the replies guys. I was assuming the user entering a string though could enter more than one word at a time & the string would end only when the user hits enter ?? |
| Cmuppet is offline | |
| | #5 |
| Compulsive Liar Join Date: Jul 2004
Posts: 149
| >I was assuming the user entering a string though could enter more than one word at a >time & the string would end only when the user hits enter ?? That's not how scanf works. Parsing only goes to the next whitespace with the %s specifier. That means that words will be split up. Fortunately, this is easy to fix, but unfortunately, it's difficult to get right. ![]() Code: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N_STRINGS 7
int cmp(const void *a, const void *b);
int main(void)
{
/* Declare an array of strings */
char strings[N_STRINGS][1024];
int i;
/* Read N_STRINGS strings */
for (i = 0; i < N_STRINGS; i++) {
/* A prompt is always helpful to users */
printf("Enter string %%%d: ", i + 1);
/* Unless you print a '\n' in the prompt, you need this */
fflush(stdout);
/* ALWAYS check scanf for success */
/* The [^\n] specifier says to read until '\n' is found */
if (scanf("%1024[^\n]", strings[i]) != 1) {
fprintf(stderr, "Error reading input\n");
return EXIT_FAILURE;
}
/* scanf leaves '\n' in the stream, so we have to remove it */
getchar();
}
/* Sort the array using cmp as the comparison routine */
qsort(strings, N_STRINGS, sizeof strings[0], cmp);
/* Print out the sorted result */
for (i = 0; i < N_STRINGS; i++) {
printf("%s\n", strings[i]);
}
return EXIT_SUCCESS;
}
int cmp(const void *a, const void *b)
{
return strcmp((const char *)a, (const char *)b);
}
In general, it's a bad idea to use scanf for string data. fgets is better suited to that task. |
| Robc is offline | |
| | #6 |
| Registered User Join Date: Jul 2004
Posts: 46
| Thanks again for your help Robc |
| Cmuppet is offline | |
| | #7 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
|
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #8 |
| Registered User Join Date: Jul 2004
Posts: 46
| Oke i've come up with this code , & i've been paining my brain trying to figure out why it's not working. Im guessing it's an array overflow somewhere somehow ??? Thanks again for helping !! Code: #include <stdio.h>
#include <string.h>
#define N_STRINGS 7
#define SIZE 15
void main(void)
{
int i,j,compare;
char arrays[N_STRINGS][SIZE];
char sortedarray[N_STRINGS][SIZE];
for (i = 0;i < N_STRINGS;++i){
printf("Enter string %d : ",i);
scanf("%s",arrays[i]);
}
printf("Printing the strings again ...\n");
for (i = 0;i < N_STRINGS;++i){
printf("\n%s\n",arrays[i]);
}
printf("Sorting the strings...\n");
for (i = 0;i < N_STRINGS;++i){
for (j = 0;j < SIZE;++j){
compare = strcmp(&arrays[i][j],&arrays[i + 1][j]);
if ((compare == -1)||(compare == 0)){
sortedarray[i][j] == arrays[i][j];
sortedarray[i + 1][j] == arrays[i + 1][j];
}
else{
sortedarray[i][j] == arrays[i + 1][j];
sortedarray[i + 1][j] == arrays[i][j];
}
}
}
printf("Printing the strings again sorted this time...\n");
for (i = 0;i < N_STRINGS;++i){
for (j = 0;j < SIZE;++j)
printf("%s",sortedarray[i][j]);
}
}
int strcmp(const char *s1,const char *s2){
return (*s1,*s2);
}
Last edited by Cmuppet; 07-21-2004 at 03:51 AM. |
| Cmuppet is offline | |
| | #9 | |||
| Helper Join Date: Jun 2004
Posts: 255
| Quote:
Code: Compiling MAIN.C: Error MAIN.C 7: main must have a return type of int Warning MAIN.C 31: Code has no effect Warning MAIN.C 32: Code has no effect Warning MAIN.C 36: Code has no effect Warning MAIN.C 37: Code has no effect Warning MAIN.C 54: Parameter 's1' is never used Quote:
Quote:
I don't pretend that this will fix all your bugs, but it's a start.
__________________ Emmanuel Delahaye "C is a sharp tool" | |||
| Emmanuel Delaha is offline | |
| | #10 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Quote:
[edit] Although, their implementation ... leaves much to be desired. ![]() After a second, it's probably a bad idea to include "string.h" in attempting to do so... ![]() [/edit] Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? Last edited by quzah; 07-21-2004 at 04:34 AM. | |
| quzah is offline | |
| | #11 |
| Registered User Join Date: Jul 2004
Posts: 46
| oh dear my brain must have been fried lol , using == instead of = thanks for pointing that out , i overlooked it completly |
| Cmuppet is offline | |
| | #12 |
| Registered User Join Date: Jul 2004
Posts: 46
| Im still getting an error message |
| Cmuppet is offline | |
| | #13 |
| Registered User Join Date: Jul 2004
Posts: 46
| Thanks btw for pointing these things out to me |
| Cmuppet is offline | |
| | #14 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Well post your latest code and actual error messages then
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading console input (character mode applications) | maxhavoc | Windows Programming | 12 | 11-27-2005 04:13 AM |
| MFC: need varying number of user input files - best interface? | BrianK | Windows Programming | 4 | 04-21-2004 04:18 PM |
| comparing user input | lambs4 | C Programming | 5 | 12-15-2002 10:28 AM |
| Am I going insane? How do you input strings? | GrNxxDaY | C++ Programming | 16 | 07-27-2002 09:53 PM |
| Format User Input When using strings | Unregistered | C Programming | 4 | 03-08-2002 03:59 AM |