Guys, could someone help me to make a program that will display this not using if-else statement.

I want to learn how to make it using for loop statement and any statement that will make this program short. I need 1 Program using for loop and another program but not if-else.

Sample Output:
_____________________________________________________________

Enter a number(0-25) : 7(Example of inputted number)

0 - Zero
1 - One
2 - Two
3 - Three
4 - Four
5 - Five
6 - Six
7 - Seven

_______________________________________________________________

Thanks a lot. ;)

Just store the words in a std::vector<std::string> and then output the appropriate word with std::cout << words[i]; . You could alternatively use a switch statement.

In the future, please use more descriptive thread titles!

David

Start by building a while loop that counts up to the entered number. Once you figure that part out, and post your effort, we'll be able to help you more.

Just store the words in a std::vector<std::string> and then output the appropriate word with std::cout << words[i]; . You could alternatively use a switch statement.

In the future, please use more descriptive thread titles!

David

I agree with you Dave, but since he said is newbie I think he should start with std::string array[] before moving to STL

Start by building a while loop that counts up to the entered number. Once you figure that part out, and post your effort, we'll be able to help you more.

I agree, and I suggest you skim through this tutorial to get you head up

I would also suggest sticking with arrays for now, especially since you have a set index. Something like this might get you going:

int num = 0;
string ary[25] = {"One", "Two", "Three", ... , "Twenty-Five"};

cout << "Give me a number:  " << flush ;
cin >> num;

for (int i = 0 ; ... ; ...)
    cout << num << " - " << ary[...] << endl ;

I've left blanks for you to fill in.

commented: I like the blanks... +2

Now I know how to do that program. :) Thanks guys. ;) You've been such a great help for me. :)

string ary[25] = {"One", "Two", "Three", ... , "Twenty-Five"};
Question: correct me if I'm wrong. I think it should be like this right?
----> string ary[26] = {"Zero","One", "Two", "Three", ... , "Twenty-Five"}

cout << "Give me a number: " << flush ;
Question: What's the use of flush?


for (int i = 0 ; ... ; ...)
Question: If i do this is it right?
-----> for (num=0,ary=0;num<=25,ary<=26;num++,ary++)

cout << num << " - " << ary[...] << endl ;
Question: ary[26]?

>>Question: correct me if I'm wrong. I think it should be like this right?
----> string ary[26] = {"Zero","One", "Two", "Three", ... , "Twenty-Five"}

Probably.

>>Question: What's the use of flush?
In order for an output stream to actually output, its buffer needs to be "flushed". The use of the "flush" manipulator is simply a manual (as opposed to automatic) flush of the output stream.

>>Question: If i do this is it right?
-----> for (num=0,ary=0;num<=25,ary<=26;num++,ary++)

ABSOLUTELY NOT. If that even compiles you'll completely hose access to your array and introduce a memory leak. This is the proper way:

const int ARY_SIZE = 5;
int myArray[ARY_SIZE] = {0, 1, 2, 3, 4};
for (int i = 0; i < ARY_SIZE; ++i) {
  cout << myArray[i];
}

>>Question: ary[26]?
Segmentation fault due to attempting to access an invalid array member.

edit> sorry for double post fbody :P

----> string ary[26] = {"Zero","One", "Two", "Three", ... , "Twenty-Five"}
You can do that also, it doesn't matter.

It's doing this,

//pseudo
ary[25]  .....  this equates to 

ary[0]
ary[1]
ary[2]
.
.
.
ary[23]
ary[24]

//THEN - compiler does this

ary[0] = "whatever is first"
ary[1] = "whatever is next"
.
.
.

//So - you could actually do this

ary[25] = {"Ten", "Eleven", "Seven", "Horse", "Chair", ...}

//and be completely legal as far as the compiler is concerned.

cout << "Give me a number: " << flush ;
For lack of a detailed description, flush as opposed to endl just sets the cursor at the end of your line to make input cleaner. So instead of,

Please enter your name:
Bobby Joe

You can have something like,

Please enter your name: Bobby Joe

-----> for (num=0,ary=0;num<=25,ary<=26;num++,ary++)
Not at all, sorry. Here's a hint - you only use one statement in each blank. So take out everything and re-look at my for() loop. Remember, if you start at 0 and your array is only 25 items large, your indexing doesn't go all the way to 25, right? So even in the case of trying to access ary[25], it would blow up.

Your for loop should look like this

for (int i = 0 ; i < ... ; i++)

Question: ary[26]?
Sorry, wrong again here. You want to output what is in the current index according to your for() loop. So remember, your variable i is starting at 0 (look at your for() loop) - so as i is incremented, you can use that to index your array, right?

>>Question: If i do this is it right?
-----> for (num=0,ary=0;num<=25,ary<=26;num++,ary++)
ABSOLUTELY NOT. If that even compiles you'll completely hose access to your array and introduce a memory leak. Eliminate the statements related to ary.

So how can I loop the words on the ary? I must display it vertically.

>>Question: ary[26]?
Segmentation fault due to attempting to access an invalid array member.

I mean it should be like this? cout << num << " - " << ary[26] << endl ;

int num = 0;
string ary[26] = {“Zero”,"One", "Two", … , "Twenty-Five"};

cout << "Give me a number: " << flush ;
cin >> num;

for (num = 0, ary = 0 ; num<=25, ary <=26 ; num++, ary ++)
cout << num << "\t - \t" << ary[26] << endl ;

How about this? :(

My post overlaps somewhat with fbody, but should help answer your remaining questions.

edit> noticed a typo in my first post - modified part about the for() loop slightly.

>>Question: If i do this is it right?
-----> for (num=0,ary=0;num<=25,ary<=26;num++,ary++)
ABSOLUTELY NOT. If that even compiles you'll completely hose access to your array and introduce a memory leak. Eliminate the statements related to ary.

So how can I loop the words on the ary? I must display it vertically.

By creating an output statement that accesses the array elements properly, like I showed you in my previous post. Also, see your question below. You obviously already know the answer to this one.

>>Question: ary[26]?
Segmentation fault due to attempting to access an invalid array member.

I mean it should be like this? cout << num << " - " << ary[26] << endl ;

Your format seems appropriate for the desired output, but you still are not accessing your array elements correctly. In addition, even if it was the correct technique, you are accessing element 26 of an array that ends at element 25. This is a major error that can cause a system crash. You need to use a variable as the subscript, not a constant/literal.


int num = 0;
string ary[26] = {“Zero”,"One", "Two", … , "Twenty-Five"};

cout << "Give me a number: " << flush ;
cin >> num;

for (num = 0, ary = 0 ; num<=25, ary <27 ; num++, ary ++)
cout << num << "\t - \t" << ary[26] << endl ;

How about this?

for (int i = 0 ; i < ... ; i++)

Why should I use a "i" variable? What is it for?
I used variable "num" for the input of the number and variable "ary" for the array.

Sample Output:
_____________________________________________________________

Enter a number(0-25) : 7(Example of inputted number)

0 - Zero
1 - One
2 - Two
3 - Three
4 - Four
5 - Five
6 - Six
7 - Seven

_______________________________________________________________

Why should I use a "i" variable? What is it for?

The variable 'i' is a generic integer variable used as an index value when accessing your array. It could just as easily be called "currentIndex".

for (num = 0, ary = 0 ; num<=25, ary <27 ; num++, ary ++)
cout << num << "\t - \t" << ary[26] << endl ;

How about this?

*sigh* You're not listening... all you're doing is throwing out wild guesses, and you've made the problem worse. That is not how programming works. Programming is logical and methodical, not willy nilly (most of the time). Take the time to think it through and understand what we are telling you. For the third time now, take all references to "ary" out of your loop's header, they do not belong there.

The statement ary[26] is completely incorrect. Compile your program and try to run it. I guarantee you that you'll get invalid output, if it even runs. You are correct in that you need to use ary, but you are incorrect in how you use the subscript (the part between the "[]"). The value between the braces must be a variable, not a constant/literal.

*sigh* You're not listening... all you're doing is throwing out wild guesses, and you've made the problem worse. That is not how programming works. Programming is logical and methodical, not willy nilly (most of the time). Take the time to think it through and understand what we are telling you. For the third time now, take all references to "ary" out of your loop's header, they do not belong there.

The statement ary[26] is completely incorrect. Compile your program and try to run it. I guarantee you that you'll get invalid output, if it even runs. You are correct in that you need to use ary, but you are incorrect in how you use the subscript (the part between the "[]"). The value between the braces must be a variable, not a constant/literal.

Is this what you meant? :

for (num = 0, i = 0 ; num<=25, i <27 ; num++, i ++)
cout << num << "\t - \t" << ary << endl ;


:D

That's a step closer.

The output statement is now formatted correctly and you are accessing the array correctly. Do not touch it again unless instructed to do so.

Now you need to fix the header of your for loop. The variable "num" is a user input that you need to use as a reference. As written, you are constantly changing the value of num, which you do not want to do. What you need to do is create an index variable (perhaps called "i"). This index variable is the only variable that you should change while the loop is running (notice I didn't say "use", there is a big difference). Observe this example:

//assume inputValue is an integer and that results is an array of integers
//also assume that resultCount is an integer indicating the size of
//the results array

do {
  cout << "Enter number of results to show: ";
  cin >> inputValue;
} while (inputValue > resultCount); //verify that inputValue is not bigger than the array

//execute a for loop that displays the first inputValue elements of the array
for (int currentIndex = 0; currentIndex < inputValue; ++currentIndex) {
  cout << "Result " << currentIndex << " = " << results[currentIndex] << endl;
}

we actually just had to code this in my class, here ill get you my code

#include <iostream> 
#include <string>
using namespace std;

int main()

{
    int number = 10;

  
cin >> number; 
	
switch (number) 
{
case 1:
    cout << "one" << endl;
    break;
 case 2:
	cout << "Two" << endl;
	break;
case 3:
	cout << "Three" << endl;
	break;
case 4:
	cout << "Four" << endl;
	break;
case 5:
	cout << "Five" << endl;
	break;
case 6:
	cout << "Six" << endl;
	break;
case 7:
	cout << "Seven" << endl;
	break;
case 8:
	cout << "Eight" << endl;
	break;
case 9:
	cout << "Nine" << endl;
	break;
case 10:
	cout << "Ten" << endl;
	break;

    default:
	    cout << "number is too large" << endl;
    break;
       
}
    return 0;
}
1.
      #include <iostream>
   2.
      #include <string>
   3.
      using namespace std;
   4.
       
   5.
      int main()
   6.
       
   7.
      {
   8.
      int number = 10;
   9.
       
  10.
       
  11.
      cin >> number;
  12.
       
  13.
      switch (number)
  14.
      {
  15.
      case 1:
  16.
      cout << "one" << endl;
  17.
      break;
  18.
      case 2:
  19.
      cout << "Two" << endl;
  20.
      break;
  21.
      case 3:
  22.
      cout << "Three" << endl;
  23.
      break;
  24.
      case 4:
  25.
      cout << "Four" << endl;
  26.
      break;
  27.
      case 5:
  28.
      cout << "Five" << endl;
  29.
      break;
  30.
      case 6:
  31.
      cout << "Six" << endl;
  32.
      break;
  33.
      case 7:
  34.
      cout << "Seven" << endl;
  35.
      break;
  36.
      case 8:
  37.
      cout << "Eight" << endl;
  38.
      break;
  39.
      case 9:
  40.
      cout << "Nine" << endl;
  41.
      break;
  42.
      case 10:
  43.
      cout << "Ten" << endl;
  44.
      break;
  45.
       
  46.
      default:
  47.
      cout << "number is too large" << endl;
  48.
      break;
  49.
       
  50.
      }
  51.
      return 0;
  52.
      }

That's not the program that i meant. LOL. :D if that's the program i can do it. :)
And you're not supposed to put a whole program or answer on a question. :) You should only help them to learn. Not giving away answer. :P Btw, thanks too. ;)

do 
{
cout << "Enter number of results to show: ";
cin >> num;

} 
while (num > 25);

for (int currentIndex = 0; currentIndex < num; ++currentIndex) 
{
cout << "Result " << currentIndex << " = " << results[currentIndex] << endl;
}

Is this it?

btw, where can i download a free Microsoft Visual Basic 6.0 with c++?

No.

You didn't even try to get it right, you just directly C/P'd my previous example and changed one variable name. Did you even take the time to look at and understand it? Tell me how any of the output from that code satisfies the requirements of your assignment. Here's a hint, IT DOESN'T, it was just an example, you need to take the concept(s) contained in it and rewrite it to fit your program. Hence the statement

Observe this example:

Think, man... I just cussed someone out for spoon feeding. Do you really think I would do it too?

>>btw, where can i download a free Microsoft Visual Basic 6.0 with c++?
Do you mean to tell me that you're trying to learn C++ without a compiler to test your work? :icon_rolleyes:
Here...

Think, man... I just cussed someone out for spoon feeding. Do you really think I would do it too?

I think this is the right answer. :)
Then my problem is solved right? :))

main ()
{

int num; // input
string ary[26] = {“Zero”,"One", "Two", … , "Twenty-Five"};

cout << "Give me a number: " << flush ;
cin >> num;

for (int i = 0 ; i <= num ; i++)
cout << i << "\t - \t" << ary[i] << endl ;

return 0;
}

That should accomplish the task, but you don't have any way to verify the input. What happens if I enter 30?

0 - Zero
1 - One
...
25 - Twenty-Five
26 - ***crash***

0 - Zero
1 - One
...
25 - Twenty-Five
26 - alfjasasldkfj
27 - aldfjkalfejoi
28 - aslvmwaovn
29 - avlkjmeaowvnwv
30 - askljvawpovnnvmoawenpaorijavwpimaouhgvaeruoifugoanvuiagag eguhav

I don't think you want either of those to happen. This is what the first loop you created is for:

do 
{
cout << "Enter number of results to show: ";
cin >> num;
 
} 
while (num > 25);

BUT, you need to adjust your prompt to fit the requirements of the assignment.

main ()
{

int num; // input
string ary[26] = {“Zero”,"One", "Two", … , "Twenty-Five"};

cout << "Give me a number: " << flush ;
cin >> num;

if (num>=0&&num<=25)
{
for (int i = 0 ; i <= num ; i++)
cout << i << "\t - \t" << ary[i] << endl ;
}

else
 cout<<"Invalid!"<<endl;

return 0;
}

>>you need to adjust your prompt to fit the requirements of the assignment.

Pay attention to his suggestion here. The user needs to know what they're inputting. Try changing your prompt to something more like,

Give me a number (0-25):

To get even a little fancier here, you could do something like,

int main ()
{

	int num; // input
	string ary[26] = {“Zero”,"One", "Two", … , "Twenty-Five"};

	bool invalid_input = true;
	while(invalid_input)
	{
		cout << "Give me a number: " << flush ;
		cin >> num;

		if (num>=0&&num<=25)
		{
			for (int i = 0 ; i <= num ; i++)
				cout << i << "\t - \t" << ary[i] << endl ;

			invalid_input = false;
		}
		else
		{
			cout<<"Invalid!"<<endl;
			 invalid_input = true;
		}
	} 

	return 0;
}

This way you could loop until you get a valid input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.