-
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
-
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.
-
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;
-
>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;
}
-
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.
-
I believe the error is from that extra brace eth0 pointed out.