954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help on this one. :(

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. ;)

udtohan_noriel
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

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.

Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

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

udtohan_noriel
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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]?

udtohan_noriel
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

>>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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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?

Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

>>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 ;

udtohan_noriel
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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? :(

udtohan_noriel
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

>>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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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

_______________________________________________________________

udtohan_noriel
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
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 thethird 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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

*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[i] << endl ;


:D

udtohan_noriel
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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;
}
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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;
}
wakesin
Newbie Poster
8 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

Not only is that not what the OP's assignment is, you're not supposed to post up complete solutions to other people's assignments for them to C/P.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You