I ran into a bug on my program that I couldn't find on my own so I decided to transfer my project from Dev C++ to Microsoft Visual Studio.

In Dev I got away with

string sName;
cin >> sName;

But MVS doesn't like doing that. What is the substitute for comparing and saving user inputed strings?

Also:

srand(time(0));

was what I used to grab the internal clock to create random numbers, but in visual studio, it doesn't recognize that command.

Any help is greatly appreciated

Dani AI

Generated

Good catch by the responders — the breakage came from relying on permissive behavior in one tool and then moving to a stricter compiler. and were on the right track and confirmed that making the build explicit fixed the immediate errors. The broader lesson: be explicit about language features and project settings; different toolchains enforce different parts of the standard.

A couple of practical tips you can apply right away:

  • Operator extraction (cin >>) stops at whitespace, so use std::getline when you need the whole line (names with spaces, sentences, etc.).
    Example:

    std::string name;
    std::getline(std::cin, name);
  • Prefer modern C++ random utilities instead of srand/rand. They produce better-quality randomness and give clearer control over seeding and distribution. For example:

    std::mt19937 rng(std::random_device{}());
    int r = std::uniform_int_distribution<int>(1,100)(rng);

Visual Studio specifics that often trip people:

  • Many VS project templates use precompiled headers; either include the project’s pch header first or disable precompiled headers for that file.
  • Make sure the file has a .cpp extension and the project is set to compile as C++ (and set the C++ language standard to C++11 or later if you want modern features).
  • If a header isn’t found, check the project’s include directories rather than assuming the IDE will “magically” find it.

Enable a higher warning level and post exact compiler error messages when asking for help — that makes debugging much faster (as suggested). Finally, keep testing on more than one compiler or toolchain; as put it, permissive environments can hide bugs.

Recommended Answers

All 6 Replies

To use the srand function with time(0) you must include <ctime>. Where as for the first one, I think you have to use a plain char array and then convert that to type string.

It would also help if you showed the errors MSV was giving.

Maybe the problem is in the include you need to add (#include <string>)

#include <string>
#include <iostream>

using namespace std ;

int main()
{
 string sName;
 cin >> sName;
 return 0;
}

Maybe the problem is in the include you need to add (#include <string>)

#include <string>
#include <iostream>

using namespace std ;

int main()
{
 string sName;
 cin >> sName;
 return 0;
}

Ahh, I never knew you could do that :P

Compiler errors? Perhaps post them?

Are proper header files included? And are you using these from the std namespace ?

Thanks!

<ctime> and <string> were what was missing from my code. Dev C++ let me skip those for some reason.

>Dev C++ let me skip those for some reason.
Now you know why it's a good idea to test your code with multiple compilers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.