CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 9th, 2009, 03:01 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    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;
    Reply With Quote
      #2    
    Old November 9th, 2009, 03:13 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    Re: Stuck on assignment

    Please anyone help me
    Reply With Quote
      #3    
    Old November 9th, 2009, 03:20 PM
    VladimirF VladimirF is offline
    Elite Member
    Power Poster
     
    Join Date: Aug 2000
    Location: Del Mar, California, USA
    Posts: 4,558
    VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+) VladimirF has much to be proud of (1500+)
    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...
    Reply With Quote
      #4    
    Old November 9th, 2009, 03:33 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    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;
    }
    Reply With Quote
      #5    
    Old November 9th, 2009, 03:36 PM
    ZuK ZuK is online now
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,094
    ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+)
    Re: Stuck on assignment

    Code:
            for (int j = 0; j <=u.size();j++)
    u is just a plain c array. there is no such thing like size

    you could use
    Code:
            for (int j = 0; j <30 ;j++) {
    and then
    Code:
                char g=u[j];
    That would be fine if you had declared u as an array of characters

    You declared it as an array of strings
    Code:
                char g=u[j][0];
    would work

    Kurt
    Reply With Quote
      #6    
    Old November 9th, 2009, 03:40 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    Re: Stuck on assignment

    ok thx for that but now this code isnt working
    if(g==f)
    Reply With Quote
      #7    
    Old November 9th, 2009, 03:43 PM
    ZuK ZuK is online now
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,094
    ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+)
    Re: Stuck on assignment

    Quote:
    Originally Posted by cijagm View Post
    ok thx for that but now this code isnt working
    if(g==f)
    That should work.
    The brace after that is the wrong way round.
    Kurt
    Reply With Quote
      #8    
    Old November 9th, 2009, 03:48 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    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 ".-"
    Reply With Quote
      #9    
    Old November 9th, 2009, 03:51 PM
    ZuK ZuK is online now
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,094
    ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+)
    Re: Stuck on assignment

    I made just that 3 modifications and it works fine for me.
    Kurt
    Reply With Quote
      #10    
    Old November 9th, 2009, 03:55 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    Re: Stuck on assignment

    hmm
    so if you want to crypt the word APE does it show:
    .- .--. -

    /cijagm
    Reply With Quote
      #11    
    Old November 9th, 2009, 04:03 PM
    ZuK ZuK is online now
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,094
    ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+)
    Re: Stuck on assignment

    There is another error
    Code:
                    klart=klart +z[i];
    should be
    Code:
                    klart=klart +z[j];
    my output
    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]$
    Kurt
    Reply With Quote
      #12    
    Old November 9th, 2009, 04:12 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    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
    Reply With Quote
      #13    
    Old November 9th, 2009, 04:19 PM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,476
    hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+)
    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;
    }
    Reply With Quote
      #14    
    Old November 9th, 2009, 04:24 PM
    ZuK ZuK is online now
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,094
    ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+) ZuK is a glorious beacon of light (400+)
    Re: Stuck on assignment

    Just noticed that there are only 29 non empty strings defined.
    use
    Code:
            for (int j = 0; j <29;j++) {
    Kurt

    Last edited by ZuK; November 9th, 2009 at 04:29 PM.
    Reply With Quote
      #15    
    Old November 9th, 2009, 04:24 PM
    cijagm cijagm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 22
    cijagm is an unknown quantity at this point (<10)
    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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:41 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.