Help with Program, I'm a newbie and I can't find the problem

This is a discussion on Help with Program, I'm a newbie and I can't find the problem within the C++ Programming forums, part of the General Programming Boards category; I believe that I have everything written correctly in this program. It compiles fine, but when I try to run ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    2

    Help with Program, I'm a newbie and I can't find the problem

    I believe that I have everything written correctly in this program. It compiles fine, but when I try to run it It goes right past the cin statements without stopping to let me type something in, and then it runs the program like I had typed nothing in. I just started to use LInux as my full time OS, and that might be the problem, because I'm new to the oddities of programming in Linux, here is the source code.

    Code:
    #include<iostream.h>
    #include<stdlib.h>
    #include <cstdlib>
    #include <ctime>
    
    
    int main()
    {
    srand(time(0));
    unsigned short int pwrone = rand()%10;
    unsigned long int randone = (int)(rand() * 10^pwrone);
    pwrone = rand()%10;
    cout<<"Your First Encryption Number is : "<< randone <<"\n\n";
    char fake;
    cout<<"Press Enter to Continue";
    cin>>fake;
    unsigned short int pwrtwo = rand()%10;
    unsigned long int randtwo = (int)(rand() * 10^pwrtwo );
    pwrone = rand()%10;
    cout<<"Your Second Encryption Number is : "<< randone <<"\n\n";
    char message[500];
    cout<<"Please Enter the Message You Wish To Encrypt: ";
    short int messsize;
    cin.get(message, 500);
    messsize = scanf(message);
    cout<<"The size of the message is" <<messsize;
    //Start Encrypting Message
    int x=0;
    int ascii[500];
    for(int x=0; x<500; x++)
    {
    ascii[x] = message[x];
    }
    int y=0;
    int encrypted[500];
    cout<<"\n\n">
    cout<<"The Encrypted Message Is: \n";
    cout<<"\n\n";
    for(int y=0; y<500; y++)
    {
    encrypted[y] = (ascii[y] * randone * randtwo - (randone - randtwo));
    cout<<encrypted[y];
    }
    int z=0;
    int decrypterone=0;
    int decryptertwo=0;
    cout<<"\n\n";
    cout<<"Please Enter the first number you want to decrypt the above message by - ";
    cin>>decrypterone;
    cout<<"Please Enter the second number you want to decrypt the above message by - ";
    cin>>decryptertwo;
    for(int z=0; z<500; z++)
    {
    ascii[z] = ((encrypted[z] + decrypterone - decryptertwo) * decrypterone * decryptertwo);
    cout<<(char)ascii[z];
    }
    return 0;
    }
    one other thing, whenever I compile this in g++ It gives me an error message when i put down #include <iostream> instead of <iostream.h> but that doesn't happen in KDevelop or any of the windows compilers i've used, why is that?

    Thank you for your help.
    Last edited by Lyuokdea; 08-14-2002 at 10:30 PM.

  2. #2
    Sfin
    Guest
    1) About the problem with using <iostream>

    you have got to add either:

    using namespace std;

    or

    using std::cout;
    using std::cin;
    using std::endl;

    after

    #include <iostream>

    to use those functions. The <iostream.h> is the C header file, while the <iostream> is the C++ one, and it uses namespaces, so you need those using statements.

    I am sure someone else will be able to explain why you need to add those lines, and offer a better explanation.

    2) Also I don't believe you can use:

    10^pwrone

    you will need tio unclude the cmath library, and use the pow function.

    #include <cmath>

    pow( 10, pwrone );

    I am sorry though, because I can't find why your program doesn't wait for you to enter anything, when you use the cin statement.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    He's right about the powers:
    Code:
    #include <iostream>
    #include <cmath>
    
    int main ()
    {
    	std::cout << pow(5,7); // 5^7
    	return 0;
    }

  4. #4
    elad
    Guest
    iostream.h is C++, NOT C. stdio.h is the comparable C file. iostream without the .h is the new standard file when using namespaces (namespaces weren't even a part of C++ when it first came out. They have been added "relatively" recently.

    The reason your code skips input into message is because you are mixing istream methods in your code. This is a common problem for relative newcomers to C++ as many tutorials don't really explain what goes on with the various istream methods very well. I have found that textbooks tend to do a better job in this regard and would refer you to the chapter on streams in J. Liberty's book (available on line for free) for a pretty good description.

    In particular you use both >> and get() istream methods. The version of get() that you wrote is using the newline char for the third, terminating char, parameter of get() by default. Unfortunately, the previous call to >> left the newline char terminting input into the variable fake in the istream buffer several lines earlier. Since >> ignores all leading whitespace subsequent calls to >> would have worked fine. However, since your call to get() will terminate input into message before 500 char have been read in if a newline char is encountered, and a newline char is the first char it will encounter in your code, there will be nothing input into message. To fix this, place the line:

    cin.ignore();

    after the last call to >> before the call to get(). You should probably use ignore() a little more intelligently than that down the road, but for now it will probably fix your problem.

    Good luck.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    2
    thanks for everybody's help

    ~Lyuokdea

Popular pages Recent additions subscribe to a feed

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