Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Some Basic C++ Help?

Hey, guys. I need some debugging help...

I'm trying to finish up a project, and I have NO idea why this program isn't working. There aren't any actual bugs in it, just a logical error apparently....

Code:
 

//The Diamond Printer

 

#include <iostream>

using namespace std;

 

char charToPrint = ' ';

int diaWidth = 0;

int space = 0;

int printing = 0;

 

void getWidth();

void getCharacter();

 

int main()

{

    cout << "Welcome to the Diamond Printer!" << endl;

    cout << "By Sabelyn Thorpe" << endl;

    getWidth();

    cout << endl;

    getCharacter();

    cout << endl;

    for (int i = 1; i <= diaWidth; i +=2) {

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        printing = i;

        do {

            cout << charToPrint;

            printing -= 1;

        } while (printing > 0);

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        cout << endl;

    }

    for (int i = diaWidth-2; i >= 1; i -=2) {

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        printing = i;

        do {

            cout << charToPrint;

            printing -= 1;

        } while (printing > 0);

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        cout << endl;

    }

    cout << endl << "Thanks for printing!";

}

 

void getWidth()

{

    do {

        cout << "Enter the width of your diamond: ";

        cin >> diaWidth;

        if ((diaWidth <=23) && (diaWidth>=1)) {

            if (diaWidth%2==0) {

                diaWidth = diaWidth+1;

            }

        }

        else {

            cout << "Please enter a number from 1 to 23.";

        }

    } while ((diaWidth <= 1) || (diaWidth >= 23));

    return;

}

 

void getCharacter()

{

    cout << "Enter a character to print: ";

    cin >> charToPrint;

    return;

}

 

Okay, what this program is supposed to do is print a diamond on the screen based on a width and composition character the user provides.

So, output for this should look something like:

Code:
 

Enter the width of your diamond: 7

Enter a chacter to print: *

 

   *

  ***

 *****

*******

 *****

  ***

   *

 

But, what the program actually does is takes the width and character from the user... and then ends. Even if the for loops aren't running properly, it should AT LEAST print that last line in the main loop, but it just ends... I can't figure it out...

Anyone have any ideas...?
 

e

Sponsor

Make your global variables static.

[c]<div class="c" id="{CB}" style="font-family: monospace;"><ol><span style="color: #993333;">static <span style="color: #993333;">int diaWidth = <span style="color: #cc66cc;">0;
[/c]
 
I just pasted it in a cpp file, compiled it and it worked fine :shock:

Try adding this to the end of your main method:
Code:
  cin.get();

  return 0;
 
I think you are getting compile time errors and just don't know it. Your main function specifies a return type of int but you do not have a return value. I believe in most CPP compilers, that will get you an error.
 
I've tried returning a value, but it just doesn't make a difference, and 'main' can't be void. The compiler doesn't complain, it just crashes the program after I input the character value to compose the diamond.

It might be doing something in that split-second, but I don't see anything else happening at all.

Like I said, it might just be my compiler... I know that happens ocasionally.
 
The main function should return a integer, always... so just add the "return 0" at the end.
And there is no errors in your code, at least me and my compiler didnt see them.
The only problem I see, its when you run the program directly, it will close after showing the diamond too fast, so you cant see it... to fix it, add this before the "return 0" shit:
Code:
cin.get();

With it, the program will wait till the user inputs another value or press enter
 
It's not working. I don't know what the hell is going on...

Code:
 

/*Sabelyn Thorpe

The Diamond Printer*/

 

#include <iostream>

using namespace std;

 

static char charToPrint = ' ';

static int diaWidth = 0;

static int space = 0;

static int printing = 0;

 

void getWidth();

void getCharacter();

 

int main()

{

    cout << "Welcome to the Diamond Printer!" << endl;

    cout << "By Sabelyn Thorpe" << endl;

    getWidth();

    cout << endl;

    getCharacter();

    cout << endl;

    for (int i = 1; i <= diaWidth; i +=2) {

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        printing = i;

        do {

            cout << charToPrint;

            printing -= 1;

        } while (printing > 0);

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        cout << endl;

    }

    for (int i = diaWidth-2; i >= 1; i -=2) {

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        printing = i;

        do {

            cout << charToPrint;

            printing -= 1;

        } while (printing > 0);

        space = (diaWidth-i)/2;

        do {

            cout << " ";

            space-=1;

        } while (space > 0);

        cout << endl;

    }

    cout << endl << "Thanks for printing!";

    cin.get();

    return 0;

}

 

void getWidth()

{

    do {

        cout << "Enter a width between 1 and 23: ";

        cin >> diaWidth;

        if ((diaWidth <=23) && (diaWidth>=1)) {

            if (diaWidth%2==0) {

                diaWidth = diaWidth+1;

            }

        }

    } while ((diaWidth < 1) || (diaWidth > 23));

    return;

}

 

void getCharacter()

{

    cout << "Enter a character to print: ";

    cin >> charToPrint;

    return;

}

 

I added it, and it's just ignoring me, apparently...
 
I don't know what a clean+build is. I've just been compiling it, then running it.
I'm using a program called Portable Dev C++. It was recommended to me by an instructor at the college I'm attending because Visual Studios had so many compatibility problems with my browser software.
 

e

Sponsor

There's a "clean" operation and a "build" operation; clean+build would just be cleaning (removing all intermediate files) and then re-compiling.

Alternatively there's usually a Rebuild option which does that. I don't think it'd be a problem here though.
 
I don't think so either... I tried it and it doesn't make a difference.

If it's working for you guys, then the code obviously works. Hopefully, when I turn it in it'll work for my instructor as well. I'm the only one in there who knowsa real programming language so I'm the only one who has to do this. ^_^
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top