Jump to content
  • Advertisement
Sign in to follow this  
Megafox

Basic C Question

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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??

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
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 ) ) );
...
}

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!