Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
#include<cstdlib>
using namespace std;

int main() {

	// Providing a seed value
	srand((unsigned)time(NULL));

	// Get a random number
	int random = 1 + (rand() % 10);

	// Print the random number
	cout << random << endl;

	return 1;
}


What I have tried:

it works online but not on microsoft visual studio
Posted
Updated 9-May-23 8:38am
Comments
Dave Kreskowiak 9-May-23 23:38pm    
The code works just fine in MS VC++.

You're going to have to supply the error message you're getting if you want help to get this fixed.

1 solution

Probably, it does. Just too quickly for you to see.
Your code creates a console app, which if if press "Run" in VS opens a console, writes to it, and closes the console window - probably faster that the paint requests reach the console itself. So it runs and ends before anything becomes visible!

Try adding a breakpoint at the return line and run it in the VS debugger. You should be able to see what it happening then.
 
Share this answer
 
Comments
Xavier Verbockhaven 9-May-23 14:56pm    
It says error in studio but not online
jeron1 9-May-23 15:25pm    
Care to share what error is?

Edit: Returning 1 from the main() signals an error, try returning 0 instead.
Xavier Verbockhaven 10-May-23 12:57pm    
dont know it is showing more then 250 errors
OriginalGriff 10-May-23 15:26pm    
That many probably means syntax errors - so start here:
How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900