Diamond in the rough

This is a discussion on Diamond in the rough within the C++ Programming forums, part of the General Programming Boards category; I have been wrestling with this code for one year, I no longer have an infinite loop, but I also ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Question Diamond in the rough

    I have been wrestling with this code for one year, I no longer have an infinite loop, but I also do not have a diamond either, something is wrong but I dont know what.

    half = number/2;
    if(number % 2 == 0)
    {
    number=(half * 2 ) + 1;
    }
    line = 0;
    charsToPrint = 1;
    spacesToPrint= half;

    while (!(line == half))
    {
    cout << spacesToPrint << character << '\n';
    spacesToPrint--;
    character+2;
    line++;

    }

    while (line == half)
    {
    cout << line << character << '\n';
    line++;
    spacesToPrint++;

    }
    while (!(line >= number))
    {
    line++;
    charsToPrint-2;
    spacesToPrint++;
    cout << spacesToPrint << character << '\n';
    }
    It's all about LOVE

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Unhappy ++

    I have lost sleep. hair and one letter grade behind this program, this and the calendar one i wrote, everyday in that starts on monday and every month has 31 days.
    It's all about LOVE

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Post the complete program and tell us what it is you want it to do.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Unhappy

    trying to make this thing print out a diamond
    i e @
    @@@
    @

    #include <iostream>

    int main()
    {

    int line, spacesToPrint, half, number;
    char charsToPrint, character;

    cout << "This program will print a diamond composed of a character, "
    "that extends the " << '\n';
    cout << " width of a specified number." << '\n';
    cout << "Please enter a odd number greater than one (example 3,7,9...) "
    << '\n';
    cin >> number;
    cout << " And a character of your choice (example @, %, #...)." << '\n';
    cin >> character;
    cout << '\n' << '\n';
    cin.ignore (10, '\n');

    half = number/2;
    if(number % 2 == 0)
    {
    number=(half * 2 ) + 1;
    }
    line = 0;
    charsToPrint = 1;
    spacesToPrint= half;

    while (!(line == half))
    {
    cout << spacesToPrint << character << '\n';
    spacesToPrint--;
    character+2;
    line++;

    }

    while (line == half)
    {
    cout << line << character << '\n';
    line++;
    spacesToPrint++;

    }
    while (!(line >= number))
    {
    line++;
    charsToPrint-2;
    spacesToPrint++;
    cout << spacesToPrint << character << '\n';
    }
    cout << "Designed by Loranne Wish" << '\n';
    cout << "Press enter to continue.";
    cin.get();

    return 0;
    }
    It's all about LOVE

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    254
    this should help
    Code:
    #include <iostream>
    using namespace std;
    void print(int rowsize);
    int main()
    {
    	int rowsize;
    	do{
    		cout<<"Please type in a positive odd integer: ";
    		cin>>rowsize;
    	}while((rowsize%2==0)||(rowsize<=0));
    	print(rowsize);
    	return (0);
    }
    void print(int rowsize)
    {
    	int spaces=rowsize/2, currentsize=1, counter, count;
    	for(counter=0;counter!=(rowsize+1)/2;counter++){
    		for(count=spaces;count!=0;count--)
    			cout<<" ";
    		spaces--;
    		for(count=currentsize;count!=0;count--)
    			cout<<"*";
    		currentsize+=2;
    		cout<<endl;
    	}
    	currentsize=rowsize-2;
    	spaces+=2;
    	for(counter=rowsize/2;counter!=0;counter--){
    		for(count=0;count!=spaces;count++)
    			cout<<" ";
    		spaces++;
    		for(count=currentsize;count!=0;count--)
    			cout<<"*";
    		currentsize-=2;
    		cout<<endl;
    	}
    }

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    thanks???

    Your right it does work, problem is:
    1. the program specifically called for the user to enter a character of his/her choosing.
    2. this has to be written in a while loop, not a do-while loop, I really appreciate the help ( as more of my hair falls out and I lose more sleep)

    my code (if you can call it that) outputs this

    3@
    2@
    1@
    3@
    2@
    3@
    4@

    Designed by Loranne Wish
    Please press enter to continue.
    It's all about LOVE

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    254
    1. the program specifically called for the user to enter a character of his/her choosing.
    2. this has to be written in a while loop, not a do-while loop, I really appreciate the help ( as more of my hair falls out and I lose more sleep)
    look at my code even a monkey could make those changes.

    look closely its really really easy to change....

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Unhappy

    well mooooo then, cause I can not convert it. Did I mention that I am a newbie?, guess not. anyway I can do the if/else and the break statements but this loop has me more than a little loopy. Basically what I need it the inputted character to increase per row for the top and decrease for the bottom, my logic is way off and has been for a year, I am retaking this class for the second time and it looks like I'll be back in it next year.
    It's all about LOVE

  9. #9
    listeninclass
    Guest
    "Did I mention that I am a newbie?"

    and

    "has been for a year, I am retaking this class for the second time "

    so not that much of a newbie - you must have had the loop lesson at least twice now

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    actually this program is in chapter 6, and we are still in chapter 3, trying to work this out ahead of time. so I guess I should be considered a novice ( i know enough to cause my system to crash, but not enough to control the logic behind a loop)
    It's all about LOVE

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    All right, lets try breaking your problem down into manegeable steps. This is the whole point of structured programming. Write a function that takes three paramiters; the number of spaces, number of characters, and the character and prints out the spaces, characters and a newline;
    Code:
    void print_line(int spaces, int chars, char ch) {
    // two loops would work well here.
    }
    Once you have that working write two loops that repeatedly call print_line, one for the top half of the diamond, followed by one for the bottom.
    some minor points
    num+2; does not change num
    num = num+2; adds 2 to num
    num += 2; same thing.

    You don't need to track how many lines you have printed, the top half of the diamond ends when there are no spaces or the number of characters == width and the dimond ends when you have no more characters to print out. The number of characters is always width - twice the number of spaces, so you don't really need to keep track of the number of characters for each line. This may only confuse you.

    Finally, here is a function I wrote for no particular reason.
    Code:
    void countdown(int n) {
        int count = n;
        while(count > 0) {
            cout << count << endl;
            --count;
        }
    }
    This seems to stop when count is one, I wonder what would happen if I used (count <= 0)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  3. Red Diamond - Little bit of a preview
    By MidnightlyCoder in forum Game Programming
    Replies: 6
    Last Post: 05-19-2006, 10:00 AM
  4. making a diamond using functions
    By Nikisha in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2003, 08:05 PM
  5. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21