6 Replies - 378 Views - Last Post: 05 March 2012 - 06:48 PM Rate Topic: -----

#1 stefan1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 09

working with arrays project >_<

Posted 03 March 2012 - 09:46 PM

hello guys i come to you seeking some help i been doing this project and i need some help understanding this stuff.
the PDF has the limitations and what im supposed to do for each option and the zip has the executable as to how is supposed to run and look.


my 1st problem is this do while loop that i enclosed put on */
im trying to keep the input running until i get a Y,y,n,N and when it receives any of those it goes into the if statements and quits the do while loop i dont understand why is it not working
im doing if choice not equal to y or Y or n or N show the message that you need to select y or N
if choice 2 happens to be y,Y,n,N i want it to quit and go to the right if statement
what im doing wrong for this part?
   /*do{
035	    cin>>choice2;
036	    if(choice2!="y" || choice2!="Y" || choice2!="n" || choice2!="N")
037	    {cout<<"please select Y or N";}
038	 
039	    }while(choice2=="y" || choice2=="Y" || choice2=="n" || choice2=="N");
040	    */ //my poor attempt at keeping the option repeating until user input y,Y,N,n


for my second part im having problem is working with arrays is confusing as hell >_<
my idea for the second part is to have the user input
the # of shifts and i will substract by that amount
and shift letters respectivey
this is my idea for example i input the message hello,there!!!
it gets stored in message[140]; and now i want to shift my message by the # of shifts
my idea is to store the new message after is shifted into a new array but im confused about the syntax or how does that work i get all sorts of errors.

i want to do this
char newarray[140];
cin>>shift
for (int i=0;i<140;i++)
newarray=message[i]-shift; //new array would be a char to

the problems that i see here are the following how do i declare properly the newarray to save the value?
for this part is it newarray[]=message[i]-shift; or newarray[140]=message[i]-shift;??
some help on that would be extremely appreciated

thank you guys






#include <iostream>
using namespace std;
void intro(); //protoype
void intro()//function intro message describing what program does
{cout <<"COP2271 Encrypter/Decrypter\nThis program is supposed to decrypt and encrypt messages using\nthe Caesar Cipher algorithm.\n\n";
}

void menu()
{cout <<"Main Menu\n\n1. To input a message\n2. To encrypt a message\n3. To decrypt a message.\n4. To perform a statistical analysis of the message\nPress 0 to quit.\n\nPlease choose: ";
}

int main()
{   char loweralphabet[27]={"abcdefghijklmnopqrstuvwxyz"}; //array containing all the lowercase letters of the alphabet
	char upperalphabet[27]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; //array containing all the uppercase letters of the alphabet
    int choice; //variable for menu to choose options
	char choice2; // variable for option 1 if it wants to overwrite message
	char message[140]; //array where message is to be stored and max is 140 characters as specified
	char message2[140];
    int shift; 
    int on=0;
    intro();
    do{
    
    menu();
    cin >> choice;

    if(choice==1){
	if(on==1){cout<<"Currently, your message is:\n";
	int i=0;
    for(i=0;message[i]!='\0';i++)
    cout<<message[i];
	cout<<endl<<"Are you sure you want to overwrite it? (Y/N)";
	cin>>choice2;
	/*do{
	cin>>choice2;
	if(choice2!="y" || choice2!="Y" || choice2!="n" || choice2!="N")
	{cout<<"please select Y or N";}

	}while(choice2=="y" || choice2=="Y" || choice2=="n" || choice2=="N");
	*/ //my poor attempt at keeping the option repeating until user input y,Y,N,n
	
	if (choice2=='y' || choice2=='Y')   
	{//on=0; past code put as reference
	cout<<"Please give new message: ";
	cin.get();
    cin.getline(message,sizeof(message));
	cout << endl;
	
	} // turns on to 0 which makes it ask for message again

	}

	if(choice2=='n' || choice2=='N') 
	{cout<<"Going back to main menu\n";  // if user decides to not overwrite message display that
	cout<< endl;}

	if(on==0)
    {cout << "Please give message: "; //ask user for message to be stored in array
	cin.get();
    cin.getline(message,sizeof(message)); //takes input from user and saves to message array
	on=1;}
    }
    
    else if(choice==2){  //if statement if option 1 hasn't been run yet 
    if(on!=1)
    cout<< "Please give a message first using option 1.\n\n"; 
    
    else{ //if option 1 has run at least once then proceed to do shift
    cout << "Please give the number of shifts to the left: ";
    cin >> shift;
	
	// get message array do + shift to the whole message by doing a for or a while loop
	// then store this value into a new array that is a char if not values will be in ascii after summation
	// 
	//display new messag inside the new array with shift
	}
    }
      
                
              
	else if(choice==3)
    {if(on!=1) //if statement if option 1 hasn't been run yet 
    cout<< "Please give a message first using option 1.\n\n";
	// if option 1 has been run once proceed to decryp it
	else{cout<<"test";
	//grab array with shifted message and do opposite calculation to cancel it out
	//store array back into another array
	//cout array after modification
	
	
	}
    }
    
	else if(choice==4)
    {if(on!=1) //if statement if option 1 hasn't been run yet 
    cout<< "Please give a message first using option 1.\n\n";
	// do option 4 nd proceed to test 
	else{cout<<"test";}
    }

	else if(choice==0)
	{cout<<"Now quitting..";
	cin.ignore();
	cin.get();

	}
                
	else{cout<<"Wrong choice, please choose again.\n\n";
	}

    
    
    
    }while(choice!=0);
    
    
    
    return 0;   
}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: working with arrays project >_<

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: working with arrays project >_<

Posted 04 March 2012 - 06:04 AM

There are two immediate problems which I can see with the code you've commented out
if(choice2!="y" || choice2!="Y" || choice2!="n" || choice2!="N")

First, you're comparing a char with a string literal. single characters need to be in single quotes.


Your other problem is that of logic. Your if statment will return true if ..
  • choice2 is not equal to 'y, or
  • choice2 is not equal to 'Y', or
  • choice2 is not equal to 'n', or
  • choice2 is not equal to 'N'

So,
if choice2 has a value of 'X', it'll evalute to true because it's not equal to 'y'
if choice2 has a value of 'y', it'll evalute to true because it's not equal to Y'
if choice2 has a value of 'Y', it'll evalute to true because it's not equal to y'
if choice2 has a value of '#', it'll evalute to true because it's not equal to 'y'

In other words, it won't ever evaluate to false.



Side suggestion - it's usually not a good idea to use "raw" arrays as strings - they come along with a whole bunch of hidden gotchas waiting to bite you, and they're not very flexible.
in C++ you're better off using the string library instead; it might make your life easier when manipulating your string data too (because you can add characters to a string on-the-fly, and you can use other common operations such as find/replace too). I created a thread on the subject of C++ strings a while ago here: http://www.dreaminco...2209-c-strings/

This post has been edited by Bench: 04 March 2012 - 06:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Re: working with arrays project >_<

Posted 04 March 2012 - 06:34 AM

I have done things like this, may not be perfect but it works for simple programs and ignores invalid input:


cout << " (Y)es or (N)o? (0 to exit) :> ";
char choice;

while(1){ //basically an endless loop

    cin >> choice;
    if (choice == '0')
        break; // get out of the endless loop

    if (choice == 'N' || choice == 'n'){
        // do the no stuff here
    }
    
    else if (choice == 'Y' || choice == 'y'){
        // do the yes stuff here
    }

    else
        continue; // go back to the start of the loop

}


Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 650
  • View blog
  • Posts: 2,223
  • Joined: 31-December 10

Re: working with arrays project >_<

Posted 04 March 2012 - 11:50 AM

I prefer a slight variant to what Bryston posted:
int main()
{
     char ans = 'y';
     
     while(ans == 'y' || ans == 'Y')
     {
          // Put code here...

          cout << "Try Again?(y/n)> ";
          cin >> ans;    
     }
}


Was This Post Helpful? 0
  • +
  • -

#5 stefan1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 09

Re: working with arrays project >_<

Posted 05 March 2012 - 05:55 PM

thanks for replies guys i was able to solve the issue of the do while not working properly.

now the problem that i have i dont know if it is a error of logic or my sintax that is stopping me from being able to do this.

i ask the user for a message which i store on char message[140];
so for example if users types bcd is store in the array message[140];
the problem i have now is with trying to shift the letters
i ask the user for #of shifts which is store on variable shift.

im doing this
for(int i=0;i<140;i++)
{message2[i]=message[i]-shift;
cout<<message2[i];} 


since message 2 is a char array it transforms ascii value back to what it was
and i would get something like this
Posted Image

i just dont know how to handle the spaces and case of a and z

any help is appreciated i been trying to solve this all day and i cant figure it out

i was also trying this

for(int i=0;i<140;i++)
{if(message[i]!='')
{message2[i]=message[i]-shift;}
cout<<message2[i];} 
[/code]

just doesnt work >_<
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: working with arrays project >_<

Posted 05 March 2012 - 06:42 PM

It's not just a and z, it's any letter that might get shifted off the end of the alphabet, right?

Google "clock arithmetic" and browse through some of the pages that produces.

As far as space is concerned, you have to decide what you want to do about that. There is no particular rule, so before you can write that part of the code you have to decide what result you want.
Was This Post Helpful? 0
  • +
  • -

#7 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: working with arrays project >_<

Posted 05 March 2012 - 06:48 PM

You can't do i<140, because the message can be lesser than 140 characters. You want to:
int i =0;
while(message[i]!='\0')
{
    //code here
    i++;
}



Also as you are creating that message2, you need the ending '\0' character after you finish your shifting loop.

This post has been edited by shurd: 05 March 2012 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1