• Advertisement

Archived

This topic is now archived and is closed to further replies.

Need help with loop...code included

This topic is 5270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am still learning C++ and wondering if any of you could help me with a loop. Here is what I want to build, it is out of a textbook, but I just bought it to learn and can not do this program at all. Here are the topics I have done so far in the book so you dont say stuff I do not know, trying to learn in the books order. Looping, Functions, Scope of Identifiers, Switch statements, Do-While, For, Break and Continue, User defined simple styles(enum and typedef), type coercion. Here is what the question says... ---------------------------------------------------------------- Write a program that reads a single character from A through Z and produces output in the shape of a pyramid composed of the letters up to and including the letter that is input. The top letter in the pyramid should be A and on each level, the next letter in the alphabet should fall between two copies of the letter that was introduced in the level above it. For example, if the input is E the output looks like this: ____A ___ABA __ABCBA _ABCDCBA ABCDEDCBA the lines are so you can see it the right way. ---------------------------------------------------------------- Ok so here is my stuff. First used enum and made the alphabet so I could link to it and increment. Then I made the function to convert to it and then be able to print it(Might be an easier way but I do not know it yet). I then got the loops set up to be able to print the pyramid with just the inputed letter and it worked fine. Now I got it where it can increment the letter to the next one. Forms sort of a weird triangle but this is where I am stuck. These loops are hard as heck to form, but I figure if I do this and do all the examples I will build a good solid base of the language before I move on. I am taking a class next semester so that should help also. Below is my code so far, if anyone has any ideas please show or link to anything that will help. -----------------------------------------------------------------

#include <iostream.h>
#include <stdlib.h>
#include <cctype>

using namespace std;

enum Alphabet{Alpha, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India,
              Juliet, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo,
              Sierra, Tango, Uniform, Victor, Wiskey, Xray, Yankee, Zulu};

Alphabet ConversionVal(char);
char print_char(Alphabet);
void letter_count(Alphabet, int&);

int main()
{
    int height, charspace, blankspace, main_loop, blanks_loop, char_loop;
    char type, print_letter, blank;
    Alphabet holder, current_holder, holder_freeze;

    current_holder = Alpha;

    height = 0;

    cout << "Please enter a letter: ";
    cin >> type;

    holder = ConversionVal(type);

    letter_count(holder, height);

    for(int main_loop = 0; main_loop < height; main_loop++ )
       {
           blankspace = height - main_loop;
           blank = ' ';
           for(int blanks_loop = 0; blanks_loop < blankspace; blanks_loop++ )
               {
                   cout << blank;
               }

           charspace = 2 * main_loop + 1;
           int char_loop = 0;
           for(Alphabet current_holder = Alpha; char_loop < charspace; char_loop++)
               {
                   if(current_holder != holder)
                       {
                           print_letter = print_char(current_holder);
                           cout << print_letter;
                           current_holder = Alphabet(current_holder + 1);
                       }

               }
           cout << endl;
        }


      //holder = Alphabet(holder + 1);


      print_letter = print_char(holder);

      cout << height;
      cout << print_letter;



      system("PAUSE");
      return 0;
}

void letter_count(Alphabet bet_count, int& height)
{

    for(height = 1; bet_count != Alpha; height++)
    {
       bet_count = Alphabet(bet_count - 1);
    }
    return;
}
Alphabet ConversionVal(char design)
{
    switch(design)
    {
        case 'A' : return Alpha;
        case 'B' : return Bravo;
        case 'C' : return Charlie;
        case 'D' : return Delta;
        case 'E' : return Echo;
        case 'F' : return Foxtrot;
        case 'G' : return Golf;
        case 'H' : return Hotel;
        case 'I' : return India;
        case 'J' : return Juliet;
        case 'K' : return Kilo;
        case 'L' : return Lima;
        case 'M' : return Mike;
        case 'N' : return November;
        case 'P' : return Papa;
        case 'Q' : return Quebec;
        case 'R' : return Romeo;
        case 'S' : return Sierra;
        case 'T' : return Tango;
        case 'U' : return Uniform;
        case 'V' : return Victor;
        case 'W' : return Wiskey;
        case 'X' : return Xray;
        case 'Y' : return Yankee;
        case 'Z' : return Zulu;
    }
}

char print_char(Alphabet test)
{
    switch(test)
    {
        case Alpha : return 'A';
        case Bravo : return 'B';
        case Charlie : return 'C';
        case Delta : return 'D';
        case Echo : return 'E';
        case Foxtrot : return 'F';
        case Golf : return 'G';
        case Hotel : return 'H';
        case India : return 'I';
        case Juliet : return 'J';
        case Kilo : return 'K';
        case Lima : return 'L';
        case Mike : return 'M';
        case November : return 'N';
        case Papa : return 'P';
        case Quebec : return 'Q';
        case Romeo : return 'R';
        case Sierra : return 'S';
        case Tango : return 'T';
        case Uniform : return 'U';
        case Victor : return 'V';
        case Wiskey : return 'W';
        case Xray : return 'X';
        case Yankee : return 'Y';
        case Zulu : return 'Z';
    }
}


[edited by - Plexmark on August 15, 2003 4:53:06 PM] [edited by - Plexmark on August 15, 2003 5:42:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
Did that compile? half of the variables are undefined.

Edit: Maybe put your code in the [ source ] [ /source ] tags (without the spaces), The HTML can mess things up.


An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on August 15, 2003 5:08:59 PM]

Share this post


Link to post
Share on other sites
IIRC you can use math operations on single chars so it would be something like this (pseudocode):

main(...)
{
int myChar;
cin << myChar;
//test here to see if it''s between a-z. look up the ASCI codes

for (char c="a"; c!=myChar; c++)
{
for (char d=myChar; d!=c; d--)
{
cout << "_";
}
for (d="a"; d!=c; d++)
{
cout << d;
}
for (d=c-1; d>"a"; d--)
{
cout << d;
}
cout << "\n";
}
system("pause");
return 0;
}

Correct me if i''m wrong though. It''s been a looong time since I used single chars.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!

Share this post


Link to post
Share on other sites
Nice little problem

Heres my solution, think it works as you need it too


#include<iostream>
#include<cstdlib>


int main()
{

char alpha[]="abcdefghijklmnopqrstuvwxyz";
char input;
std::cin>>input;

for(int i=0;i<strlen(alpha);++i)
{
int j=0;
for(;j<i;++j)
{
std::cout<<alpha[j];
}
for(;j>=0;--j)
{
std::cout<<alpha[j];
}

std::cout<<"\n";
if(alpha[i]==input)break;
}
system("pause");
return(0);
}

Share this post


Link to post
Share on other sites
quote:
Original post by quant
Nice little problem

Heres my solution, think it works as you need it too

[ code ... ]


Very nicely done!

[edited by - Tiffany Smith on August 15, 2003 5:38:56 PM]

Share this post


Link to post
Share on other sites
Thanks for the help.

I was thinking to use arrays would make it easy, I think that is what you used quant. Just wanted to do it without since I wasnt there yet and was trying to do the user defined to solve it.

I wish there was a book that taught you how to create for statements. But I guess practice makes perfect.

Back to coding to try to get good at this for loop deal.

Thanks for you help again...if you know any info on how to develop for statements, it will be welcomed.

Plexmark

Share this post


Link to post
Share on other sites
Hi again

You can modify my last example to use a switch function if you dont want to use the array, although i would recommend reading about arrays, makes things a lot easier. Heres the new code, works the same as the old one.


#include<iostream>
#include<cstdlib>

//prototype for function

const char* returnname(const int& i);


int main(int argc, char* argv[])
{
char input;
std::cin>>input;

for(int i=0;i<26;++i)
{
int j=0;
for(;j<i;++j)
{
std::cout<<returnname(j);
}
for(;j>=0;--j)
{
std::cout<<returnname(j);
}

std::cout<<"\n";
if(*returnname(i)==input)break;

}

system("pause");
return(0);

}

const char* returnname(const int& i)
{
switch(i)
{
case 0:return"a";
case 1:return"b";
case 2:return"c";
case 3:return"d";
case 4:return"e";
case 5:return"f";
case 6:return"g";
case 7:return"h";
case 8:return"i";
case 9:return"j";
case 10:return"k";
case 11:return"l";
case 12:return"m";
case 13:return"n";
case 14:return"o";
case 15:return"p";
case 16:return"q";
case 17:return"r";
case 18:return"s";
case 19:return"t";
case 20:return"u";
case 21:return"v";
case 22:return"w";
case 23:return"x";
case 24:return"y";
case 25:return"z";
default: return"failed";
}
return"failed";
}


I also noticed that you used iostream.h in your code, you shouldnt use this header, it is obsolete and should be replaced with iostream, notice there is no .h . stdlib.h is also obsolete, and should be replaced with cstdlib. Hope that helps .

[edited by - quant on August 15, 2003 6:28:24 PM]

Share this post


Link to post
Share on other sites
const char* returnname(const int& i);

do not understand this code, have never seen this. It is a const I understand that. What is the * after the char for?

And what does making the function a const do for it? Or is that just a personal preference?

----------------------------------------------------------------

int main(int argc, char* argv[])

I have never seen anyone add arguments to the int main().

and what are the argc and argv[]. I do not see them anywhere else inside the program.

----------------------------------------------------------------

std::cout<
what is the std:: from?

-----------------------------------------------------------------


sorry for the 20 questions, just trying to work on code tonight and seem to be learning alot. Thanks for any answers you can provide.

Plexmark

[edited by - Plexmark on August 15, 2003 6:37:13 PM]

Share this post


Link to post
Share on other sites
Your book should cover all of this, if its any good

const char* is the return type of the function, it is a character pointer which points to something which is const, and so cannot be modified, read up on pointers in your book, or google it .

The arguments in main() dont need to be there, but argc is a count of the number of strings on the command line, argv is an array that contains pointers to each of the strings that were entered on the command line, that isnt really important though, especially in this example, i just usually include it though(habbit).

std is a namespace, again google.com or your book for more info on that. Most of the standard library functionality is in the std namespace, and you need to qualify it somewhere in your program, either by typing "using namespace std;" at the top of your code, or by qualifying it explicitly on the standard classes that you use, e.g. std::cout, std::string etc.

Hope that helps a bit , if this stuff isnt in your book, i would buy a better one

[edited by - quant on August 15, 2003 7:10:26 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by quant
Nice little problem


Heres my solution, think it works as you need it too


#include<iostream>
#include<cstdlib>


int main()
{

char alpha[]="abcdefghijklmnopqrstuvwxyz";
char input;
std::cin>>input;

for(int i=0;i<strlen(alpha);++i)
{
int j=0;
for(;j<i;++j)
{
std::cout<<alpha[j];
}
for(;j>=0;--j)
{
std::cout<<alpha[j];
}

std::cout<<"\n";
if(alpha[i]==input)break;
}
system("pause");
return(0);
}




Damn, that IS clever. Short and sweet - nice.

Share this post


Link to post
Share on other sites

  • Advertisement