Basic C Question

Started by
13 comments, last by bakery2k1 18 years, 4 months ago
Hi. I've just recently started to dabble with C and I have a simple question for you guys. What is the difference if any between printf/scanf and cout/cin ??? Which method is better used to create a simple DOS console program??
Advertisement
If you are working with C, you can't use cout and cin, and so you'd have to use printf and scanf...

However, if you working with C++, you can use both, but personally I prefer printf and scanf.

regards,
cout/cin are C++ objects. printf/scanf are C functions. Which is better depends largely on what the DOS program is and your personal preferences, although given you say C in your topic I suppose you have to go with printf/scanf.
Thank you for your replies. Yes, Im starting out small with plain C at the moment, so printf and scanf is the way to go for now.
Instead of using scanf you should try fgets and sscanf, scanf doesn't handle malformed input well, plus it's more prone to overrunning your buffers.
Ive decided to go with cout and cin for now.. since i found an excellent C++ tutorial on the net...

Ok my first guessing game and it's a typical console C++ app. Now the problem im having is that it is "randomly" generating the number 41 the whole time... am i not using the RAND function properly?? or what?

#include <stdlib.h>#include <iostream>using namespace std;int main(int argc, char *argv[]){    int ComputerNumber = rand()%100;    int GuessNumber = -1;    int NoOfTries = 0;        cout<<"Welcome to the Guessing Game!\n";            while(GuessNumber!=ComputerNumber && NoOfTries < 8)    {        cout<<"Choose a number.\n";        cin>> GuessNumber;        if (ComputerNumber>GuessNumber)        cout<<"The number is too small"<<endl;            if (ComputerNumber<GuessNumber)        cout<<"The number is too large"<<endl;                NoOfTries++;    }        if(GuessNumber==ComputerNumber)        cout<<"You guessed the number!";    else        cout<<"Sorry the number was"<<ComputerNumber;    return 0;}
Using rand is basically nothing more than returning the next entry in a long array of predefined numbers. If you use it once every time the application starts, it will always return the same number.
You can set its seed by using srand(number). This number could be anything as long it is somewhat "random" (a date for example).
In Windows this would work:
srand(GetTickCount());printf("number = %d", rand());
Without windows I think you normally use

#include "time.h"

srand( time(0) );
Quote:Original post by Bezben
Without windows I think you normally use

#include "time.h"

srand( time(0) );

In C++, you should use C++ headers, and C++-style casts:

#include <cstdlib>#include <ctime>int main() {   using std::rand;   using std::srand;   using std::time;   ...   srand( static_cast< unsigned >( time( 0 ) ) );   ...}
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Ok I understand that you need to seed the random number generator which is great, but I want the program to have an upper limit to generate to, for instance 100.

With the time seed this value can go into the thousands which isnt what i want. Sorry, but im a bit of a perfectionist and I need to understand.

thanks for all the replies though :)

Ok got it to work... here is my ammended code...

int main(int argc, char *argv[])
{
srand( (unsigned)time ( NULL ) ); //NEW LINE SRAND SEED WITH TIME

int ComputerNumber = rand()%100; // GIVE RAND BUT LIMIT TO 100...
int GuessNumber = -1;
int NoOfTries = 0;
}

This topic is closed to new replies.

Advertisement