Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
See more:
Can anyone convert this C program into C++, and make it print any amount not only 1 to 3 like Enter 1 to A B C..Z , Enter 5 to AAAAA...BBBBB.....ZZZZZ, and other like this.

MIDL
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,k,n;
    

printf("Enter number(1-3)");

scanf("%d",&n);

if(n>=1 && n<=3)
{
    for(i=65;i<91;i++)
    {
         if(n==1)
         printf("\t %c",i);
    
        for(j=65;j<91;j++)
        {
           if(n==2)
           printf("\t %c%c",i,j);
               
               for(k=65;k<91;k++)
               {
                 if(n==3)
                 printf("\t %c%c%c",i,j,k);
                }
        }
      }
}
system("pause");
return 0;
}
Posted
Updated 22-Jan-10 17:42pm
v9

This has nothing to do with C++. If you really don't want to use C here, then replace the calls to printf with std::cout and scanf with std::cin.

And just ensure the appropriate headers are included. There's not anything more, really.
 
Share this answer
 
Why don't you stop and think about what you're asking. You don't need recursion to do this ( although I'd pay money to see you hand that code in and have your teacher ask you to explain it ). iostream.h is NOT C++. A main() that does not return an int is not C++. Again, if your teacher has given you these ideas, your class sucks and you need to leave it. Ditto the use of printf. However, to make a C program that does what you want, just stop and think about what you're asking. You want to input a number, then work through the alphabet and print each letter X times. Looking through your code, it should be blatantly obvious why your code doesn't even do that for the numbers 1-3. If you can't work THAT out, you definitely need to give up on programming. Then stop and think. Instead of typing in code to deal with each number you want to support, what concept is ALREADY in your code that allows you to execute some code a specific number of times ?
 
Share this answer
 
'Any amount' is a bit pretentious (you've not such a computational power...). Anyway, you may do better than you did, though you've to go recursive ("To Iterate is Human, to Recurse, Divine" - L. Peter Deutsch):
void seq(char * str, unsigned int level, unsigned int depth)
{
  for (char c='A'; c<='Z'; c++)
  {
    str[level]=c;
    if ( level == depth-1 )
      printf("%s\t",str);
    else
      seq(str, level+1, depth);
  }
}
const int MAX_DEPTH = 10;
void main()
{
  char str[MAX_DEPTH+1] = {0};
  unsigned int depth;
  printf("enter a number between 1 and %d: ",  MAX_DEPTH );
  if (scanf("%u", &depth) != 1)
    return;

  depth %= MAX_DEPTH;
  seq(str,0, depth);
}


As Rajesh already noted, the choice between C or C++, is irrelevant here.
:)
 
Share this answer
 
In addition to the fact that you're using C, and not C++ ( as others have said, is void main valid C ? ), I will just add that you should expect that your teacher knows how to use google and is already aware that someone has given you some code. You should talk to your teacher before they talk to you, if you don't want to be failed for cheating.

If your teacher thinks this is valid C++, you should leave the class, and not wait to be kicked out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900