how to stop the loop

This is a discussion on how to stop the loop within the C++ Programming forums, part of the General Programming Boards category; i have done this porgram that sorts alphabets in ascending or descending order, when compiling this program, i only get ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    7

    Thumbs up how to stop the loop

    i have done this porgram that sorts alphabets in ascending or descending order,
    when compiling this program, i only get one error saying

    fatal error c1004 : unexpected end of file found.

    here is the code:



    Code:
    #include <iostream>
    #include <cctype>
    #include <cstdlib>
    
    int main()
    {
    	char	strspace[50];	/* enough ?? */
    	printf("Enter a string ");
    	scanf("%s",strspace);
    	printf("The string was >>%s<<\n",strspace);
    
    using namespace std;
    
    {  char ans;
       do
       {
          //Declarations
            char string[21];
            char decrypt;
            int i;
    
          //Get string from user
            cout<<"Enter a string no longer than 20 characters long:"<<endl;
            cin.get(string,20);
            cin.ignore(80,'\n');
            cout<<endl;
    
          //encrypt 
            for(int a = 0; a < strlen(string); a++)
            {
               if(isupper (string[a]))
                  i = (int) 'A';
               else if(islower ((int) string[a]))
                  i = (int) 'a';
    
               if(isalpha(string[a]))
               {
                  string[a] = string[a] - i;
                  string[a] = string[a] + 1;
                  string[a] = string[a] % 26;
                  string[a] = string[a] + i;
               }
            }
    
          
    
          //Ask for decrypt
            cout<<"Would you like to decrypt this string? (y,n) ";
            cin>>decrypt;
            cout<<endl;
    
          //Decrypt and output
            if(decrypt != 'n') //decrypt?
            {
               for(int j = 0; j < strlen(string); j++) //decrypt individual characters
               {
                  if(isalpha(string[j]))
                  {
                     if(isupper (string[j]))
                        i = (int) 'A';
                     else if(islower ((int) string[j]))
                        i = (int) 'a';
    
                     string[j] = string[j] - i;
                     string[j] = string[j] - 1;
                     if(string[j] < 0)
                     string[j] = 25;
                     string[j] = string[j] + i;
                  }
               }
               cout<<endl<<endl<<"The decrypted string is: "<<string<<endl<<endl;
            }
    
          //Run again code
            cout<< "Would you like to run this program again? (y,n) ";
            cin>>ans;
            cin.sync();
    
       }while(ans != 'n');
       return 0;}
    }



    can anyone investigate what the error is?

    thanks
    Last edited by amits; 05-19-2004 at 07:15 AM.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'm not even reading that until you use code tags.
    Example:
    [ code ]
    int i;
    i=5;
    [ /code ]

    But remove the spaces from within the tags.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    I'll start at the top and do a few.

    Code:
    #include <iostream>
    #include <cctype> <---don't need if you drop the C syntax below
    #include <cstdlib> <---don't need if you drop the C syntax below
    
    int main()
    {
    	char	strspace[50];	/* enough ?? */
    	printf("Enter a string ");  <----your using C syntax here ??
    	scanf("%s",strspace);
    	printf("The string was >>%s<<\n",strspace);
    
    using namespace std;  <--- should really be global here
    
    {  char ans;  <---whats the brace for?
       do
       {
          //Declarations
            char string[21];
            char decrypt;
            int i;
    
          //Get string from user
            cout<<"Enter a string no longer than 20 characters long:"<<endl;
            cin.get(string,20);
            cin.ignore(80,'\n');  <---- why 80 ?
            cout<<endl;
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >using namespace std; <--- should really be global here
    Why? A using directive only has an effect on the scope in which it's used. So a local using directive in main will only make the names visible in main. A conforming compiler will give you an error with this:
    Code:
    #include <iostream>
    
    void foo();
    
    int
    main()
    {
      using namespace std;
    
      cout<<"test1"<<endl;
      foo();
    
      return 0;
    }
    
    void
    foo()
    {
      cout<<"test2"<<endl;
    }
    My best code is written with the delete key.

  5. #5
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    why?
    Just for ease of usage until more is learnt about it. My understanding what that it was put there without fully understanding what it is.

    Its sometimes best to put it out of the way globally when your in the early stages.
    Thats the way I did it anyway and it helped me concentrate on the simple topics first.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I believe the error is from that extra brace eth0 pointed out.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 06:47 PM
  2. need help, fgets won't stop while loop
    By Enkid in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 08:15 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 06:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21