Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: C/C++ newb program(need help)

  1. #1

    Question C/C++ newb program(need help)

    I am new to programming all together and was referred to this site so as I could ask a question about a program I am trying to make(keep in mind this is a very simple program to you guys probably).

    (done in C/C++)

    #include <iostream.h>
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <stdlib.h>

    int main()
    {
    bool exitProg = false;
    while(exitProg == false)
    {
    char mainChoice;
    char inputChar[1000], outputChar[1000];
    int inputInt[1000], outputInt[1000];
    for(int b = 0; b <= 999; b++)
    {
    inputInt[b] = 0;
    outputInt[b] = 0;
    }
    int mod = 0;
    int e = 0;
    int d = 0;
    printf("Welcome to Dencoder, please type the letter of your selection:\n");
    printf("a. Encode Message \nb. Decode Message\nc. Exit Program\n");
    cin >> mainChoice;
    switch(mainChoice)
    {
    case 'a':
    printf("\n\nPlease enter your message you want encoded:\n");
    scanf("%s", &inputChar);
    printf("\nPlease enter your modifier(a number, 1-100 to help better encode your message):\n");
    cin >> mod;
    //scanf("%d", &mod);
    printf("\nYour encoded message is:\n");
    do
    {
    outputInt[e] = atoi("inputChar[e]") + mod;
    if(inputInt[e] = '\0')
    {
    outputInt[e] = '\0';
    }
    printf("%d ", &outputInt[e]);
    e++;
    }while(inputChar[e] != '\0');
    printf("\n\n\n\n");
    break;
    case 'b':
    break;
    case 'c':
    exitProg = true;
    break;
    }
    }
    return 0;
    }

    ok I need you to ignore the case b and case c for now I am just trying to get it to encode, but whenever I try that, it outputs huge numbers for my outputInt. it always outputs numbers like 12 thousand or so. and if you don't know what the atoi() command is, it takes a char's ascii number and puts it into an integer format. please respond to this, it is bugging the hell out of me and I can't figure out why it is outputting numbers like 1280049 or other high numbers like that.
    --------------------------------------------------------------------
    E, the modern pi.

  2. #2
    HeadShot Master N1nja Cybr1d's Avatar
    Join Date
    Jul 2003
    Location
    Boston, MA
    Posts
    1,840
    http://www.cs.wustl.edu/~schmidt/C++/
    http://www.cprogramming.com/

    check those links out, maybe they can help. I dont know much about programing in C/C++ because i'm majoring in Computer Engineering and we only are required to take a C class, and also I never bothered to learn on my own. I'll prolly start some day once i'm done with all the overwhelming classes i'm in. Good Luck with those links and also look around AO cuz i'm sure there's quite a few well written tutorials by some of our members.

    peace

  3. #3

    thanks

    Thanks, I have it all worked out now.
    --------------------------------------------------------------------
    E, the modern pi.

  4. #4
    Thanks, I have it all worked out now.
    Be a darling and tell us what the problem was, dont know too much about programming my self but learning is always fun. Glad you found a solution to your problem.
    [shadow] Nobody\'s perfect, but I\'m damn close...[/shadow]

  5. #5
    Junior Member
    Join Date
    May 2002
    Posts
    15
    Dwcnmv,

    Are you doing c/c++ now? I would suggest you to restrict from clubbing the languages(though C++ is from C, it is advanced). My suggestions for the above code:

    a) for getting the ascii no. (in C itself) you could have used
    printf("%d",inputChar[e])--prints the ascii char of the letter stored
    if used as "%c" will show the character

    b) From the efficiency point of view, you can use gets() function to store the text you enter. The gets() is very easy.

    The URLs following can be useful:
    www.cprogramming.com (a must mention as mentioned already)
    www.programmersheaven.com
    www.freeprogrammingresources.com
    www.oopweb.com

    Chokks.

  6. #6

    Unhappy Update of Script

    I took your advice and just set it to using 'printf("%d ", inputChar[e] + mod);' this works fine, I don't know if it works for turning ints into a char, but the hard part is getting all the numbers into it, that is the part of the code that is freezing(but compiling without warnings or errors).

    Well I have redone it quite a bit and I have the decode part done. My only problem is that, it still isn't working . The code itself I keep looking at and I can't find anything wrong, but whenever I run the program when I try to decode something, and I enter in a number and start to enter the second one, the program freezes and it pops up an error and says would you like to send error report? The program compiles fine, but for some reason it isn't working.
    Here is the code:

    #include <iostream.h>
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <stdlib.h>

    int main()
    {
    bool exitProg = false;
    do
    {
    int t = 0;
    int p = 0;
    char mainChoice;
    char inputChar[1000], outputChar[1000];
    int inputInt[1000], outputInt[1000];
    for(int b = 0; b <= 999; b++)
    {
    inputInt[b] = 0;
    outputInt[b] = 0;
    }
    int mod = 0;
    int e = 0;
    int d = 0;
    printf("Welcome to Dencoder, please type the letter of your selection:\n");
    printf("a. Encode Message \nb. Decode Message\nc. Exit Program\n");
    cin >> mainChoice;
    switch(mainChoice)
    {
    case 'a':
    printf("\n\nPlease enter your message you want encoded(use a _ for spaces):\n");
    scanf("%s", &inputChar);
    printf("\nPlease enter your modifier(a number, 1-100 to help better encode your message):\n");
    cin >> mod;
    printf("\nYour encoded message is:\n");
    do
    {
    printf("%d ", inputChar[e] + mod);
    e++;
    }while(inputChar[e] != '\0');
    printf("\n\n\n\n");
    break;
    case 'b':
    printf("Enter the numbers of the message you want decoded(press enter after each number and type '-1' when done):\n");
    for(t = 0; t != -1; t++)
    {
    scanf("%d\n", inputInt[t]);
    if(inputInt[t] == -1)
    {
    t = -1;
    }
    }

    printf("Enter the modifier of our message:\n");
    cin >> mod;
    for(p = 0; p != '\0'; p++)
    {
    inputInt[p] = inputInt[p] - mod;
    }
    do
    {
    int i;
    for(i = 32; i <= 126; i++)
    {

    if(inputInt[e] == i)
    {
    outputChar[e] = i;

    }

    }
    if(outputChar[e] == 95)
    {
    outputChar[e] = 32;
    }
    e++;
    }while(inputInt[e] != '\0');
    printf("\nHere is your decoded message:");
    printf("\n%s", outputChar);
    cout << endl << endl << endl << endl;
    break;
    case 'c':
    exitProg = true;
    break;
    }
    }while(exitProg == false);
    return 0;
    }

    Someone figure this out, cause I am really confused.
    --------------------------------------------------------------------
    E, the modern pi.

  7. #7
    Senior Member
    Join Date
    Jun 2003
    Posts
    236
    can you please be more specific about where your program crashes..

    I am assuming this is the piece of code though
    scanf("%d\n", inputInt[t]);

    remove the \n so it reads
    scanf("%d", inputInt[t]);

    also you need change the the code that reads in your text from
    scanf("%s", &inputChar);
    since inputChar is a character array you do not neet the & in front, and for better code that you can use spaces I would actually change it to:
    getline(inputChar,1000);

    now it will read up to 1000 characters includeing spaces and stop only when you hit return, it also limits the number of chars to 1000 max so that you cant be buffered overflowed(not that your using this as enterprise code but practicing safe programming is good practice)
    That which does not kill me makes me stronger -- Friedrich Nietzche

  8. #8

    Question here......

    Ok, well I did the things you said, and yea I saw that those are more effecient/better, but I am still having the problem that it doesn't work. It is freezing when you try to enter the numbers you want decoded, I enter the first one and it freezes, what is wrong with it? Here is the program if you want to run it:
    http://home.comcast.net/~dwcnmv/encoder.exe
    This is getting really frustrating.


    [glowpurple]Hello World.[/glowpurple]
    [gloworange]Hello World.[/gloworange]
    [shadow]Hello World.[/shadow]
    [pong]Hello World.[/pong]
    [blur]Hello World.[/blur]
    [flip]Hello World.[/flip]
    (wanted to test the different types of message text on the forum)
    --------------------------------------------------------------------
    E, the modern pi.

  9. #9
    Senior Member
    Join Date
    Jun 2003
    Posts
    236
    ok change

    scanf("%d\n", inputInt[t]);

    to

    scanf("%d", &(inputInt[t]));

    so are you just practicing making a caeser cipher?
    That which does not kill me makes me stronger -- Friedrich Nietzche

  10. #10

    Question ???

    What is a caeser cipher?
    --------------------------------------------------------------------
    E, the modern pi.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •