Thread: C++ Alphabetical sorting, etc Help

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Unhappy C++ Alphabetical sorting, etc Help

    Gosh I detest IE.

    Anyways.

    I'm trying to write a code that will allow me to open a file, or a text input, and then sort it alphabetically. I then want the program to be able to be stop/quit at anytime by using a scanf or something to read a key, or multiple key inputs at a time, such like windows reads Cntrl + alt + Del, I would like something similar, but reading func keys is beyond me.

    I have NO idea how to transform chars into numbers, and vice versa. I tried surfing the net for help for a few hours, and I've seen bubble sort, and the like, but they come with the IOstream which doesn't seem to work anymore, so I'm mightily confused.

    Anyways, Here is my code, I'll /* */ the sections I'm struggling in.
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h>
    
    int readWord(FILE *in, char aWord[]);
    FILE * openFile();
    int main ()
    
    {
    printf("Would you like to read from a file, or input the text yourself? \n");
    printf("Please specify: (f or i)");
    scanf("" */  I want this to be the part where you specify wether or not you want to sort from a text file, or from your own input. The file read works perfectly though, I'm not quite sure how to implement the sorting algorithm in to it*/
    {
    	char aWord[100];
    	FILE *in;
    	in = openFile();
    	while (readWord(in, aWord) != EOF)
    	{
    		printf("%s ", aWord);
    	}
    	printf("\n");
    	fclose(in);
    	return(0);
    }
    FILE * openFile()
    {
    	FILE *in;
    	char filename[1000];
    	printf("Please enter the input file name including its path\n");
    	scanf("%s", filename);
    	if ((in = fopen (filename, "r")) == NULL)
    	{
    		fprintf(stderr, "Can't open the input file.\n");
    		printf("Program is terminated ...\n");
    		exit(1);
    	}
    	return in;
    }
    int readWord(FILE *in, char aWord[])
    {
    	return fscanf(in, "%s", aWord);
    }
    
    {
    /* Here is my main problem, getting the stuff to sort. I have NO idea how to transform chars into numbers, and vice versa.  */
    
    void sort(int mynum[], int size)
    {
    	int pass, indx, hold;
    	for (pass = 0; pass < size - 1 ; pass++)
    	{
    		for (indx = 0; indx < size - pass - 1; indx++)
    		{
    			if ( mynum[indx] > mynum[indx + 1] )
    			{
    				hold = mynum[indx];
    				mynum[indx] = mynum[indx + 1];
    				mynum[indx + 1] = hold;
    			}
    		}
    	}
    }
    int main ()
    {
    	int i, mynum[10], mycopy[10];
    	srand((unsigned)time(NULL));
    	for (i = 0; i < 10; i++)
    	{
    		mynum[i] = rand();
    		mycopy[i] = mynum[i];
    	}
    	sort(mynum, 10);
    	printf("original numbers sorted numbers\n");
    	for (i = 0; i < 10; i++)
    	{
    		printf("%10d %17d \n", mycopy[i], mynum[i]);
    	}
    	return(0);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you want to convert strings to numbers use strtol().

    As for the code, it appears to me you are posting C code but the title of the thread makes it seem like you want some help with C++. This is the C forum.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also if you intend to use scanf you probably want to maybe have a variable ready to go that will store the answer.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2

    Exclamation

    Quote Originally Posted by claudiu View Post
    If you want to convert strings to numbers use strtol().

    As for the code, it appears to me you are posting C code but the title of the thread makes it seem like you want some help with C++. This is the C forum.
    Ah my bad. Sorry. I am new to forums :S

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Lucifix View Post
    Gosh I detest IE.
    Your irrelevant opinion is being duly ignored thankyou.
    but they come with the IOstream which doesn't seem to work anymore
    iostream has never been part of C, it is C++ only. It's not that it "doesn't seem to work" anymore, you just need to fiond examples that are for the language you are using.

    What's going on with this code? There appear to be two int main()'s and the first one has an extra open brace, making the rest of the code into illegal nested functions!
    Can you please modify this to make it to compile first?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Alphabetical sorting function
    By typer in forum C Programming
    Replies: 6
    Last Post: 05-20-2006, 02:35 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM
  5. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM