Like Tree1Likes

Homework due tonight! help please!!

This is a discussion on Homework due tonight! help please!! within the C++ Programming forums, part of the General Programming Boards category; ok, here it is Code: #include <string> using namespace std; #include <fstream> #include <iostream.h> #include <stdlib.h> int main() { ........ ...

  1. #16
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    ok, here it is
    Code:
    
    
    #include <string> 
    using namespace std; 
    #include <fstream> 
    
    #include <iostream.h> 
    #include <stdlib.h> 
    
    int main() 
    { 
    ........ int something; 
    ........ string infilename, 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; 
    ........ ifstream fin; 
    
    ........ char blah, andy, key[26]; 
    
    ........ for (int word = 0;word < 26;word++) { 
    
    ............ char key[26] = {'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'}; 
    
    ............ blah == key[word]; 
    
    ............ blah = tolower(blah); 
    
    ............ cout << "Please enter your replacement for character A: \n"; 
    ............ cin >> a; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character B: \n"; 
    ............ cin >> b; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character C: \n"; 
    ............ cin >> c; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character D: \n"; 
    ............ cin >> d; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character E: \n"; 
    ............ cin >> e; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character F: \n"; 
    ............ cin >> f; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character G: \n"; 
    ............ cin >> g; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character H: \n"; 
    ............ cin >> h; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character I: \n"; 
    ............ cin >> i; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character J: \n"; 
    ............ cin >> j; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character K: \n"; 
    ............ cin >> k; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character L: \n"; 
    ............ cin >> l; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character M: \n"; 
    ............ cin >> m; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character N: \n"; 
    ............ cin >> n; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character O: \n"; 
    ............ cin >> o; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character P: \n"; 
    ............ cin >> p; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character Q: \n"; 
    ............ cin >> q; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character R: \n"; 
    ............ cin >> r; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character S: \n"; 
    ............ cin >> s; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character T: \n"; 
    ............ cin >> t; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character U: \n"; 
    ............ cin >> u; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character V: \n"; 
    ............ cin >> v; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character W: \n"; 
    ............ cin >> w; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character X: \n"; 
    ............ cin >> x; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character Y: \n"; 
    ............ cin >> y; 
    ............ something++; 
    
    ............ cout << "Please enter your replacement for character Z: \n"; 
    ............ cin >> z; 
    ............ something++; 
    
    ............ if (something = 26) { 
    ............ cout << "Please input a file (input)" << endl; 
    ............ cin >> infilename; 
    ............ } 
    ........ } 
    ........ if (fin.fail()){ 
    ................ cout << "Error - file opening failed" << endl; 
    ................ } 
    ........ while (!fin.eof()) { 
    ............ if (fin.eof()) 
    ............ break; 
    ............ for (int word = 0; word < 26; word++) { 
    ............ cout << "Your new message is: \n" << key[word] << endl; 
    ............ } 
    ........ } 
    ........ fin.close(); 
    
    ........ system("PAUSE"); 
    ........ return 0; 
    }

  2. #17
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    this was my assignment..

    write a program that encodes the text in a data file using a character array named "key" containing 26 characters; this key is read from the keyboard; the first letter contains the character that is to replace the letter "a" in the data file; the second letter contains the letter that is to replace the letter "b" in the data file; and so on; assume that all punctuation is to be replaced by spaces; check to be sure that the key does not map two different characters to the same one during the encoding

    and the second part to it is...

    write a program which decodes the file that is the output of Problem 27; assume that the same integer key is read from the keyboard by this program and is used in the decoding steps; note that you will not be able to restore the punctuation characters

  3. #18
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    if (something = 26)

    That should be ==
    = is the assignment operator
    == is boolean equals

    Also, you're putting too much into that loop. For instance, the initialization of key should only happen once.

  4. #19
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Uh... you don't even try to read it in. Just use
    Code:
    cin >> key;
    .

  5. #20
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    ok, thanks, im trying that now... and joshdick.. im trying to figure out what you mean by 'the initialization of key should only happen once. '

  6. #21
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I'd recommend simply inputting a 26 char string, or if you wish to enter them individually, create a loop to input each char. Hard coding all that is...well messy.

  7. #22
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    ugh... it goes down asking for the replacement like it's supposed to and then it asks for the input file and then it asks for the replacement letters again

  8. #23
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    well, seeing as how i dont know how to do i 26 char string (i haven't been taught) it's kinda hard. i've been trying to do one this whole time, but... im guessing at how to do it.

  9. #24
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If I wanted 10 integers from a user, I'd do like so:
    Code:
    const int MAX = 10;
    int a[MAX];
    
    for(int i=0; i < MAX; ++i)
    {
       cout << "Enter an integer: ";
       cin >> a[i];
    }
    Notice how I only need to write the code to input an integer once even though I want to get 10 integers. As a matter of fact, if I wanted 1000 integers, I need only to change MAX to 1000 rather than hard-code 990 more lines of code. Do you see that now?

  10. #25
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    OOOOHHHHH.... ok, i get it... thank you, see.. that was never explained to me before... lol

  11. #26
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    You're already using filehandling and you have not yet learned how to use strings or char arrays?

  12. #27
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    Yeah, well, my prof never really explained anything... the only reason i know how to do the file opening is because someone else showed me how to do it...

  13. #28
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    the only question i have about what the one integer thing is... how do i get the program to ask for the specific replacements of the letters, like "replacement for a: " "replacement for b: ", etc...

  14. #29
    Registered User
    Join Date
    May 2003
    Posts
    82
    I'm not quite following the program logic, but it looks like you need a sub loop.

    For a given string, you could have something like:
    Code:
    // I think key is the right string
    for(int current = 0; current < SIZE; current++)
    {
        cout << "Enter replacement for " << key[current] << ": ";
        cin >> key[current];
    }
    Again, I'm not quite following the logic in your code, so that may not be helpful.
    Last edited by AH_Tze; 04-07-2005 at 12:37 PM.

  15. #30
    Registered User
    Join Date
    Apr 2005
    Posts
    23
    hm, the mystring thing is kind of confusing me, i think that's what i need, but i'm not completely sure what the mystring is equal to

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 50 percent for this homework?
    By a.mlw.walker in forum C Programming
    Replies: 2
    Last Post: 03-22-2009, 05:15 PM
  2. please help..linked list homework assignment due tomorrow..
    By rocketman03 in forum C Programming
    Replies: 2
    Last Post: 11-23-2008, 05:32 PM
  3. Help Someone! First program/errors and its due tonight!
    By LittleLotte in forum C Programming
    Replies: 4
    Last Post: 02-02-2005, 08:21 AM
  4. Its Late...Due Tonight....Help Please
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-13-2001, 02:18 PM
  5. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM

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