| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Stuck on assignment
Hello!
I am a 30 year old guy from sweden that are stuck on an assignment that are due in 2 days, the assignment is to crypt a word to non letter word, and possible back to the original word what's wrong with my code here it is: #include <iostream> #include <string> #include <cstring> #include <vector> using namespace std; string text(string a); int main() { int x; string ord; cout<< "\nV\x84lj f\x94r kryptering\n1.Text till morse\n2.Morse till Text "; cin>>x; if(x==1) cout<<"skriv in texten"<<endl; cin>>ord; cout<<"det krypterade ordet \x84r "<<text(ord); system("PAUSE"); return 0; } string text(string a) { string z[30]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---" ,"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-" ,"..-","...-",".--","-..-","--..",".--.-",".-.-","---."}; string u[30]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O" ,"P","Q","R","S","T","U","V","W","X","Y","Z","Å","Ä","Ö"}; string klart=""; for (int i = 0;i <a.size();i++) { char f =a [i]; for (int j = 0; j <=u.size();j++) { char g=u[j]; if(g==f) } klart=klart +z[i]; } } } return klart; |
|
#2
|
|||
|
|||
|
Re: Stuck on assignment
Please anyone help me
|
|
#3
|
|||
|
|||
|
Re: Stuck on assignment
Your curly braces do not match (you have too many '}', so that your last return is outside of the function).
If you format your code properly, it will be easier to see.
__________________
Vlad - MS MVP [2007, 2008, 2009] - www.FeinSoftware.com Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more... |
|
#4
|
|||
|
|||
|
Re: Stuck on assignment
yeah that 1 curly bracers i forgot but still it's not working as it should be, if i dont use arrays[]
i get the code to work but the assignment is with arrays otherwise i cant store the signs. ex string x [3]={"--","----","..."};-works string y="--","----","..."; doesnt work heres the code again #include <iostream> #include <string> #include <cstring> #include <vector> using namespace std; string text(string a); int main() { int x; string ord; cout<< "\nV\x84lj f\x94r kryptering\n1.Text till morse\n2.Morse till Text "; cin>>x; if(x==1) cout<<"skriv in texten"<<endl; cin>>ord; cout<<"det krypterade ordet \x84r "<<text(ord); system("PAUSE"); return 0; } string text(string a) { string z[30]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---" ,"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-" ,"..-","...-",".--","-..-","--..",".--.-",".-.-","---."}; string u[30]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O" ,"P","Q","R","S","T","U","V","W","X","Y","Z","Å","Ä","Ö"}; string klart=""; for (int i = 0;i <a.size();i++) { char f =a [i]; for (int j = 0; j <=u.size();j++) char g=u[j]; if(g==f) } klart=klart +z[i]; } } } return klart; } |
|
#5
|
|||
|
|||
|
Re: Stuck on assignment
Code:
for (int j = 0; j <=u.size();j++) you could use Code:
for (int j = 0; j <30 ;j++) {
Code:
char g=u[j]; You declared it as an array of strings Code:
char g=u[j][0]; Kurt |
|
#6
|
|||
|
|||
|
Re: Stuck on assignment
ok thx for that but now this code isnt working
if(g==f) |
|
#7
|
|||
|
|||
|
Re: Stuck on assignment
That should work.
The brace after that is the wrong way round. Kurt |
|
#8
|
|||
|
|||
|
Re: Stuck on assignment
ok i got it to work but now it is writing out the entire z array instead of the corresponding one ex.
If i want to derypt the letter A it should compile it to ".-" |
|
#9
|
|||
|
|||
|
Re: Stuck on assignment
I made just that 3 modifications and it works fine for me.
Kurt |
|
#10
|
|||
|
|||
|
Re: Stuck on assignment
hmm
so if you want to crypt the word APE does it show: .- .--. - /cijagm |
|
#11
|
|||
|
|||
|
Re: Stuck on assignment
There is another error
Code:
klart=klart +z[i]; Code:
klart=klart +z[j]; Code:
V�lj f�r kryptering 1.Text till morse 2.Morse till Text 1 skriv in texten APE det krypterade ordet �r .-.--..[Kurt@localhost Tests]$ |
|
#12
|
|||
|
|||
|
Re: Stuck on assignment
hmm yeah that's right and i changed it to klart=klart +z[j];
but when i run it a pop up window from visualstudio show's up talking about a Just-In-Time Debugger ans asks me if i want toset the debugger as the selected default debugger or manually choose the debugger engines. very strange never happened b4- /C |
|
#13
|
|||
|
|||
|
Re: Stuck on assignment
Along with the brace fixes, I added the extra stuff in red:
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
string text(string a);
int main()
{
int x;
string ord;
cout<< "\nV\x84lj f\x94r kryptering\n1.Text till morse\n2.Morse till Text ";
cin>>x;
if(x==1)
{
cout<<"skriv in texten"<<endl;
cin>>ord;
cout<<"det krypterade ordet \x84r "<<text(ord);
}
system("PAUSE");
return 0;
}
string text(string a)
{
char g;
string z[30]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---"
,"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-"
,"..-","...-",".--","-..-","--..",".--.-",".-.-","---."};
string u[30]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"
,"P","Q","R","S","T","U","V","W","X","Y","Z","Å","Ä","Ö"};
string klart="";
for (int i = 0;i <a.size();i++)
{
char f =a [i];
for (int j = 0; j <=30;j++)
{
string tmp=u[j];
g=tmp[0];
if(g==f)
{
klart=klart +z[j]+" ";
break;
}
}
}
return klart;
}
|
|
#14
|
|||
|
|||
|
Re: Stuck on assignment
Just noticed that there are only 29 non empty strings defined.
use Code:
for (int j = 0; j <29;j++) {
Last edited by ZuK; November 9th, 2009 at 04:29 PM. |
|
#15
|
|||
|
|||
|
Re: Stuck on assignment
Thank you very much now it works , could you explain what u did? if it isn't to much to ask.
/C |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|