Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,390 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,131 people online right now. Registration is fast and FREE... Join Now!




Having Problem with NULL

 
Reply to this topicStart new topic

Having Problem with NULL, can't get loop to work

browngod2002
27 Oct, 2006 - 05:40 AM
Post #1

New D.I.C Head
Group Icon

Joined: 16 Sep, 2006
Posts: 27



Thanked: 1 times
Dream Kudos: 275
My Contributions
I am having a problem with this loop that must terminate when the user hits the enter key. I have tried all the NULL statements that I know but it doesn't seem to work. Need some advice on how to gt it to work.

CODE

#include "stdafx.h"
#include <string.h>
#include <stdio.h>

#define MAX_STRING 26
#define MAX_SIZE 15

/* function prototype */
void BubbleSort(char myString[]);



int main(void)
{

     char myString[MAX_STRING];
     int i;

     printf_s("Enter a word:\n");
     scanf_s("%s", myString);
    
     while (myString != '\0')
     {
         scanf_s("%s",myString);
     }

    
     BubbleSort(myString);
    
     for (i = 0; i < MAX_STRING; i++)
     {
         printf("%s\n",myString[i]);
     }



     return 0;
}

void BubbleSort(char myString[])
{
     int pass;
     int i;
     int hold;
    

     for (pass = 1; pass < MAX_STRING; pass++)
     {
         for (i = 0; i < MAX_STRING; i++)
         {
             if (myString[i] > myString[i + 1])
             {
                 hold = myString[i];
                 myString[i] = myString[ i + 1];
                 myString[i + 1] = hold;
             }
         }
     }
}

User is offlineProfile CardPM
+Quote Post

ifoam
RE: Having Problem With NULL
27 Oct, 2006 - 06:24 AM
Post #2

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
this looks like c, but in c+:
CODE
while (myString != '\0')


would have to be
CODE
while(myString != NULL


i dunno if that helps any.
you might also wanna try..
CODE
while(myString != '/n'

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Having Problem With NULL
27 Oct, 2006 - 06:29 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
I am assuming you are referring to the WHILE loop. Try using the EOF constant.
CODE

while (myString != EOF)

User is offlineProfile CardPM
+Quote Post

browngod2002
RE: Having Problem With NULL
30 Oct, 2006 - 05:49 PM
Post #4

New D.I.C Head
Group Icon

Joined: 16 Sep, 2006
Posts: 27



Thanked: 1 times
Dream Kudos: 275
My Contributions
Thank you I got it to work in a different way but now I am having another problem with this program.
Here is what is supposed to happen with this program.

Use the bubble sort program given in the textbook as a starting point. Rewrite this program to sort an array of “strings” rather than an array of integers.



Write a program that will do the following:



1.) Prompt the user for words and store these words in an array.

2.) Print the words in the array after it is loaded.

3.) Sort the words in the array so that they are in alphabetical order.

4.) Print the words in the array after the sort is completed.



Your program should allow for the possible storage of up to 25 words. Each word should allow a maximum size of 15 letters. Your program should make certain that these limits are not exceeded so that the program does not crash if more words, or longer words, are entered by the user.



A sample run is shown below.

Sample Run



Enter a word: four



Do you have more words? yes or no: y

Enter a word: score



Do you have more words? yes or no: y

Enter a word: and



Do you have more words? yes or no: y

Enter a word: seven



Do you have more words? yes or no: y

Enter a word: years



Do you have more words? yes or no: y

Enter a word: ago



Do you have more words? yes or no: n





four

score

and

seven

years

ago





ago

and

four

score

seven

years

Here is what I got so far

CODE

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

#define MAX_WORDS 25
#define MAX_LENGTH 15

char repeat[4];

int main(void)
{
    int i;
    int hold;
    int pass;
    int numWords;
    char words[MAX_WORDS];
    

    printf("Do you have a word y or n: ");
    scanf("%s", repeat);

    numWords = 0;

    while (repeat[0] == 'y')
    {
        printf("Enter a word: ");
        scanf("%s",&words);
        printf("Do you have another word y or n: ");
        scanf("%s", repeat);
        numWords++;
    }
    
        for(i = 0; i < numWords; i++)
            printf("%s\n", words[i]);
        

    
    /*bubble sort*/
    for(pass = 0; pass < numWords; pass++)
    {
        for(i = 0; i < numWords; i++)
        {
            if(words[i] > words[i + 1])
            {
                hold = words[i];
                words[i] = words[i + 1];
                words[i + 1] = hold;
            }
        }
    }

    


    getchar();
    getchar();

    return 0;
}





User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Having Problem With NULL
30 Oct, 2006 - 06:22 PM
Post #5

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
Their is a liblary for string >>>cstring or string.h


and their a lots of keyword and function under it







User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Having Problem With NULL
30 Oct, 2006 - 07:07 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You should also note that you have currently declared a character array in which to hold the words...a single dimensiaonal character array will hold only one word or string. I believe you may require a two dimensional array. Something like
CODE

char [MAX_WORDS][MAX_LENGTH];

User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Having Problem With NULL
31 Oct, 2006 - 02:17 AM
Post #7

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(Amadeus @ 30 Oct, 2006 - 08:07 PM) *

You should also note that you have currently declared a character array in which to hold the words...a single dimensiaonal character array will hold only one word or string. I believe you may require a two dimensional array. Something like
CODE

char [MAX_WORDS][MAX_LENGTH];


The alternative is to use string...

Nye Nye suggested you use the standard headers...
CODE
#include <string>
using namespace std;

which is the modern way to use them.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Having Problem With NULL
31 Oct, 2006 - 05:32 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
I'd agree that the string object is the way to go, if available. I'd suggested the char array due to the fact that it appeared as if the user was programming in C, as opposed to C++. If that assumption was incorrect, and the user is programming in C++, then the string object and standard headers are the better choice. If using C, however, he will have to make do with the string.h header and the char array.
User is offlineProfile CardPM
+Quote Post

browngod2002
RE: Having Problem With NULL
31 Oct, 2006 - 07:24 AM
Post #9

New D.I.C Head
Group Icon

Joined: 16 Sep, 2006
Posts: 27



Thanked: 1 times
Dream Kudos: 275
My Contributions
QUOTE(Amadeus @ 31 Oct, 2006 - 06:32 AM) *

I'd agree that the string object is the way to go, if available. I'd suggested the char array due to the fact that it appeared as if the user was programming in C, as opposed to C++. If that assumption was incorrect, and the user is programming in C++, then the string object and standard headers are the better choice. If using C, however, he will have to make do with the string.h header and the char array.


I am programming in C
User is offlineProfile CardPM
+Quote Post

TuffGuy
RE: Having Problem With NULL
1 Nov, 2006 - 08:15 AM
Post #10

New D.I.C Head
*

Joined: 31 Oct, 2006
Posts: 6


My Contributions
QUOTE(browngod2002 @ 31 Oct, 2006 - 08:24 AM) *

QUOTE(Amadeus @ 31 Oct, 2006 - 06:32 AM) *

I'd agree that the string object is the way to go, if available. I'd suggested the char array due to the fact that it appeared as if the user was programming in C, as opposed to C++. If that assumption was incorrect, and the user is programming in C++, then the string object and standard headers are the better choice. If using C, however, he will have to make do with the string.h header and the char array.


I am programming in C


couldn't you use c strings? or just use a multi dimension array..


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Having Problem With NULL
1 Nov, 2006 - 08:16 AM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
That is exactly what is specified above.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month