Jump to content

errors in c++

Go to solution Solved by Eigenvektor,
1 hour ago, BobVonBob said:

Your while loop is also wrong. Right now it's just going to print "Input was incorrect please try again" forever. It won't prompt for a value again and it won't make it back up to where you process the choice variable.

Nice catch! I went over the line and just though it was unnecessarily verbose (why check the same options again) but didn't notice it was actually an endless loop 😄

 

Here's a working version (tested with "g++ choice.cpp -o choice")

Spoiler
#include <iostream>

int main()
{
    char choice = '\0';

    std::cout << "Running bootloader..." << std::endl;

    while (choice == '\0')
    {
        std::cout << "Pick an option to boot into OS, BIOS or Shell by typing: o, b, s" << std::endl;
        std::cin >> choice;

        if (choice == 'o')
        {
            std::cout << "Booting into OS..." << std::endl;
        }
        else if (choice == 'b')
        {
            std::cout << "Booting into BIOS" << std::endl;
        }
        else if (choice == 's')
        {
            std::cout << "Booting into Shell" << std::endl;
        }
        else
        {
            std::cout << "Input was incorrect please try again" << std::endl << std::endl;
            choice = '\0';
        }
    }
}

 

 

<#include <iostream>
 
int main(){
    char choice = '\0';
        std::cout << 'Running bootloader...' << std::endl;
        std::cin << 'Pick an option to boot into OS, BIOS or Shell by typing: o, b, s\n'
        if (choice == 'o'){
            std::cout << 'Booting into OS...' << std::endl;
 
        }
        if (choice == 'b'){
            std::cout << 'Booting into BIOS' << std::endl;
        }
        else if (choice == 's'){
            std::cout << 'Booting into Shell' << std::endl;
        }
 
        else{
            while (choice != 'o' or choice != 'b' or choice != 's'){
            std::cout << 'Input was incorrect please try again';
            }
        }
    }
   >

the errors I am getting is shown below in the screenshot(I am using the mingw compiler):

image.thumb.png.4fc82bab9174ba49a7cb02b7db3f3d42.png

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/
Share on other sites

Link to post
Share on other sites

33 minutes ago, swabro said:

still have the errors though

Please use code tags, makes stuff more readable.

 

std::cout << 'Booting into OS...' << std::endl;

You're using the wrong quotations marks here. You need to use double quotes for a string, so:

std::cout << "Booting into OS..." << std::endl;

 

A single quote is for a single character (e.g. 'a'). For a string, i.e. multiple characters, you need to use "string" instead. As the compiler is telling you, your "character constant is too long", meaning you're trying to fit multiple characters into a char, which only accepts a single character.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15696442
Share on other sites

Link to post
Share on other sites

1 hour ago, Electronics Wizardy said:

cin >> "test";

That's wrong too.

 

How is the entered value supposed to get into the choice variable? std::cin needs a variable, not a string for a prompt. If you want a prompt print it using std::cout (without a line ending) and use

std::cin >> choice;

to put the value in choice.

 

Your while loop is also wrong. Right now it's just going to print "Input was incorrect please try again" forever. It won't prompt for a value again and it won't make it back up to where you process the choice variable.

¯\_(ツ)_/¯

 

 

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 comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15696446
Share on other sites

Link to post
Share on other sites

1 hour ago, BobVonBob said:

Your while loop is also wrong. Right now it's just going to print "Input was incorrect please try again" forever. It won't prompt for a value again and it won't make it back up to where you process the choice variable.

Nice catch! I went over the line and just though it was unnecessarily verbose (why check the same options again) but didn't notice it was actually an endless loop 😄

 

Here's a working version (tested with "g++ choice.cpp -o choice")

Spoiler
#include <iostream>

int main()
{
    char choice = '\0';

    std::cout << "Running bootloader..." << std::endl;

    while (choice == '\0')
    {
        std::cout << "Pick an option to boot into OS, BIOS or Shell by typing: o, b, s" << std::endl;
        std::cin >> choice;

        if (choice == 'o')
        {
            std::cout << "Booting into OS..." << std::endl;
        }
        else if (choice == 'b')
        {
            std::cout << "Booting into BIOS" << std::endl;
        }
        else if (choice == 's')
        {
            std::cout << "Booting into Shell" << std::endl;
        }
        else
        {
            std::cout << "Input was incorrect please try again" << std::endl << std::endl;
            choice = '\0';
        }
    }
}

 

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15696491
Share on other sites

Link to post
Share on other sites

just a suggestion, why aren't you using SWITCH for this?

RELOAD BEFOR QUOTING. I EDIT MY POST ALOT!

MARK MY POST AS THE ANSWER IF I HELPED.

 

CPU: AMD Ryzen 5 5600 | GPU: RTX 2070 Super | Mobo: MSI X470 Gaming Pro | RAM: Corsair vengeance 32GB 8x2 3200mhz CL16 | SSD: Samsung QVO 870 1TB | PSU: SeaSonic FOCUS Plus 750 Gold 750 W 80+ Gold | Cooler: Thermaltake Contac Silent

12 | OS: Windows 11 Pro | Pcpartpicker: https://pcpartpicker.com/list/rGR6gb
Displays: ViewSonic XG2401 23.6" 1920 x 1080 144 Hz 

Laptop: Lenovo Ideapad Gaming i5-1130 RTX3050 16GB | OS: Windows 11 Pro

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15697705
Share on other sites

Link to post
Share on other sites

8 hours ago, HelloPeople said:

just a suggestion, why aren't you using SWITCH for this?

switch vs if... else if... else structures are just a matter of preference.  They're functionally the same when compiled.  Both cause branching to occur.  I kind of dislike the additional keywords needed for switch statements, for example.  Still need to use braces, so technically less typing and line breaks needed for the if... else if... else structure.  And most IDEs/editors seriously freak when trying to copy/paste multiple switch cases, especially VS Code.  Fixing indentation isn't my idea of a good time.

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15698212
Share on other sites

Link to post
Share on other sites

12 hours ago, Vicarian said:

switch vs if... else if... else structures are just a matter of preference.  They're functionally the same when compiled.  Both cause branching to occur.  I kind of dislike the additional keywords needed for switch statements, for example.  Still need to use braces, so technically less typing and line breaks needed for the if... else if... else structure.  And most IDEs/editors seriously freak when trying to copy/paste multiple switch cases, especially VS Code.  Fixing indentation isn't my idea of a good time.

ty for the clarification 

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15699184
Share on other sites

Link to post
Share on other sites

16 hours ago, Vicarian said:

switch vs if... else if... else structures are just a matter of preference.  They're functionally the same when compiled.  Both cause branching to occur.  I kind of dislike the additional keywords needed for switch statements, for example.  Still need to use braces, so technically less typing and line breaks needed for the if... else if... else structure.  And most IDEs/editors seriously freak when trying to copy/paste multiple switch cases, especially VS Code.  Fixing indentation isn't my idea of a good time.

switch makes the code cleaner and easier to read IMO. 😅

RELOAD BEFOR QUOTING. I EDIT MY POST ALOT!

MARK MY POST AS THE ANSWER IF I HELPED.

 

CPU: AMD Ryzen 5 5600 | GPU: RTX 2070 Super | Mobo: MSI X470 Gaming Pro | RAM: Corsair vengeance 32GB 8x2 3200mhz CL16 | SSD: Samsung QVO 870 1TB | PSU: SeaSonic FOCUS Plus 750 Gold 750 W 80+ Gold | Cooler: Thermaltake Contac Silent

12 | OS: Windows 11 Pro | Pcpartpicker: https://pcpartpicker.com/list/rGR6gb
Displays: ViewSonic XG2401 23.6" 1920 x 1080 144 Hz 

Laptop: Lenovo Ideapad Gaming i5-1130 RTX3050 16GB | OS: Windows 11 Pro

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15699347
Share on other sites

Link to post
Share on other sites

3 minutes ago, swabro said:

u are right but the reason I am not using for three conditionals its not really worth it correct me if I am wrong though

you are not wrong. its a matter of preference. 

 

for me readable/easy to read codes are very important. 

so I tend to use switch case more than if/else when there's 3 or more conditions.

RELOAD BEFOR QUOTING. I EDIT MY POST ALOT!

MARK MY POST AS THE ANSWER IF I HELPED.

 

CPU: AMD Ryzen 5 5600 | GPU: RTX 2070 Super | Mobo: MSI X470 Gaming Pro | RAM: Corsair vengeance 32GB 8x2 3200mhz CL16 | SSD: Samsung QVO 870 1TB | PSU: SeaSonic FOCUS Plus 750 Gold 750 W 80+ Gold | Cooler: Thermaltake Contac Silent

12 | OS: Windows 11 Pro | Pcpartpicker: https://pcpartpicker.com/list/rGR6gb
Displays: ViewSonic XG2401 23.6" 1920 x 1080 144 Hz 

Laptop: Lenovo Ideapad Gaming i5-1130 RTX3050 16GB | OS: Windows 11 Pro

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15700976
Share on other sites

Link to post
Share on other sites

18 minutes ago, HelloPeople said:

you are not wrong. its a matter of preference. 

 

for me readable/easy to read codes are very important. 

so I tend to use switch case more than if/else when there's 3 or more conditions.

I agree this tends to be a very subjective thing and in coding and in my life I am not very organized 😅

Link to comment
https://linustechtips.com/topic/1473250-errors-in-c/#findComment-15701006
Share on other sites

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

×