Jump to content

c++ Code won't run in visual studio.

Go to solution Solved by Mattattack94,

I copied the code and got it to run by changing line 13 from "scanf" to "scanf_s" and by removing the "%f"

"scanf_s("%f", &balance, &maxLimit, &desiredLimit);"

Code won't run in visual studio, but runs on online compilers for c++. Trying to make a program for myself to calculate credit Limits, just for fun.

 

 

// Example program
#include <iostream>
#include <string>

int main()
{
    float balance = 0;
    float maxLimit = 0;
    float needToPay = 0;
    float desiredLimit = 0;
    
    //Balance Finder
    printf("Enter Your Balance, Max Credit Limit, and a percentage that is under 30. \n");
    scanf("%f %f %f", &balance, &maxLimit, &desiredLimit);
    desiredLimit = desiredLimit / 100;
    printf("Your Balance is %f \n Your Max Credit Limit is %f \n Your Chosen Percentage is %f \n", balance, maxLimit, desiredLimit);
    needToPay = ((desiredLimit * maxLimit) - balance) * -1;
    desiredLimit = desiredLimit * 100;
    printf("To get your Balance to %f Percent, You would need to pay %f", desiredLimit, needToPay);
    return 0;
}

 

image.png.2a2a1102af7de122dc91edbc3a83f2a8.png

Link to comment
https://linustechtips.com/topic/1212563-c-code-wont-run-in-visual-studio/
Share on other sites

Link to post
Share on other sites

2 minutes ago, Skilled Loaf said:

Code won't run in visual studio, but runs on online compilers for c++. Trying to make a program for myself to calculate credit Limits, just for fun.

 

 

// Example program
#include <iostream>
#include <string>

int main()
{
    float balance = 0;
    float maxLimit = 0;
    float needToPay = 0;
    float desiredLimit = 0;
    
    //Balance Finder
    printf("Enter Your Balance, Max Credit Limit, and a percentage that is under 30. \n");
    scanf("%f %f %f", &balance, &maxLimit, &desiredLimit);
    desiredLimit = desiredLimit / 100;
    printf("Your Balance is %f \n Your Max Credit Limit is %f \n Your Chosen Percentage is %f \n", balance, maxLimit, desiredLimit);
    needToPay = ((desiredLimit * maxLimit) - balance);
    desiredLimit = desiredLimit * 100;
    printf("To get your Balance to %f Percent, You would need to pay %f", desiredLimit, needToPay);
    return 0;
}

 

image.png.2a2a1102af7de122dc91edbc3a83f2a8.png

What does "not run" mean? Compile errors, exe wont start, no output, does a breakpoint innstart of code hit?

 

It might be missing argc, argv in main; perhaps the gnu does pickup main(void) prototype but msvc not, might try adding them.

Link to post
Share on other sites

2 minutes ago, Skilled Loaf said:

It used to give me "Unable to start Program, system cannot find the specified file" Now it just says build failed.

Sounds more like project setup issue maybe.

 

Id use the new project wizard to make a new command line project; have it generate a working project for you, find the main function and transplant your code in there.

 

Link to post
Share on other sites

Just now, Skilled Loaf said:

it also is giving me this. I am sort of a noob in c++, but I don't understand how this code works in online c++ compilers, but not in visual studio.

 

image.thumb.png.9e85c9fdff84dc12db54ab5b4a2f64ec.png

That warning (these days perhaps configured as error, or show errors as warnings is on for the project) is just a warning that that form/type of function is deprecated (due to security vonerabilties because it handles user input in a potentional dangerous way prone to buffer/pointer overrun attacks) and better means are available. Its not somethjng to worry about when just studying the language so for the purpose of your study you could maybe suppres that using a #pragma directive.

 

Link to post
Share on other sites

12 minutes ago, Skilled Loaf said:

it also is giving me this. I am sort of a noob in c++, but I don't understand how this code works in online c++ compilers, but not in visual studio.

 

image.thumb.png.9e85c9fdff84dc12db54ab5b4a2f64ec.png

That's a deprecated code warning. Basically scanf isn't best practice to use for a production codebase because it can cause buffer overflows, but given this code is only being used by you it can be disabled it in two ways.

 

The first, is to add

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

This will disable all warnings about insecure and deprecated functions. The ifdef part checks to see if it's running in Visual Studio, then if it is it defines the flag to disable the warnings.

 

The second option is to use

#pragma warning(disable:4996)

This can be used to suppress any warnings based on their warning code from the first column in the log.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to post
Share on other sites

4 minutes ago, Skilled Loaf said:

What i've noticed is that the default code works without any problems.

 

image.png.b22f4dd4ee38caceb547b0e52871fecd.png

If with the other code you didnt resolve that error the build stops after compilation and wont link (no exe formed).

 

Did you try what that error message said for disabling unsafe crt checks? That should help.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×