By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,433 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,433 IT Pros & Developers. It's quick & easy.

need help with For Loop.

P: n/a
hello, i am new to programming... i was looking for some help if
anybody can help me here, i'd glady appreciate it.

i have to create a for structure where it asks the user to input a
certain integer.
after the user inputs that integer it tells you to input more values
but only the amount of the first integer you put in.

So if you put in 5. It would ask you to input only 5 more values.

How would I go about doing this?

Thanks,
Nicolas.

Dec 19 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
nicolas napsal:
hello, i am new to programming... i was looking for some help if
anybody can help me here, i'd glady appreciate it.

i have to create a for structure where it asks the user to input a
certain integer.
after the user inputs that integer it tells you to input more values
but only the amount of the first integer you put in.

So if you put in 5. It would ask you to input only 5 more values.

How would I go about doing this?

Thanks,
Nicolas.
Hi.

This is minimalistic example without any error handling:

#include <iostream>

int main()
{
int count;
std::cin >count;

for (int i = 0; i < count; ++i)
{
int value;
std::cin >value;

// Do anything you need with value here
}
}

And something with (very basic) error handling:

#include <iostream>

int main()
{
int count;
std::cin >count;

for (int i = 0; std::cin.good() && i < count; ++i)
{
int value;
std::cin >value;

// Do anything you need with value here
}

if (!std::cin.good())
{
std::cerr << "Some error occured\n";
return EXIT_FAILURE;
}
}

Dec 19 '06 #2

P: n/a

nicolas wrote:
hello, i am new to programming... i was looking for some help if
anybody can help me here, i'd glady appreciate it.

i have to create a for structure where it asks the user to input a
certain integer.
after the user inputs that integer it tells you to input more values
but only the amount of the first integer you put in.

So if you put in 5. It would ask you to input only 5 more values.

How would I go about doing this?

Thanks,
Nicolas.
What do you mean by structure? What have you tried?
A program that collects an integer x and counts through a for loop
x-times should be a fairly simple task.

#include <iostream>
#include <ostream>
#include <limits>

int main()
{
std::cout << "Enter the count:";
int count;
std::cin >count;
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');
for(int n = 0; n < count; ++n)
{
std::cout << "count: " << n << std::endl;
}
return 0;
}

Dec 19 '06 #3

P: n/a
On 19 Dec 2006 11:53:14 -0800 in comp.lang.c++, "nicolas"
<np*********@gmail.comwrote,
>i have to create a for structure where it asks the user to input a
certain integer.
after the user inputs that integer it tells you to input more values
but only the amount of the first integer you put in.

So if you put in 5. It would ask you to input only 5 more values.

How would I go about doing this?
What did you try? What happened?

Making the user count things ahead of time is a bad idea. That's
something computers are better at. Preferable is something like:

std::vector<intvalues;
int input;
while(cin >input)
values.push_back(input);
std::cout << "I got " << values.size() << " numbers.\n";
Dec 19 '06 #4

P: n/a
nicolas wrote:
>[Do my homework redacted]
You can find your answer at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Dec 19 '06 #5

P: n/a
Hi Nicolas:

This is simply the perfect solution for your question

#include<iostream.h>
int main()
{ int UserNum;
cout << "Enter the User Number:"; // To guide the user to enter the
first value
cin >UserNum;

for(int n = 1; n <= UserNum; ++n)
{
int temp;
cout << "Enter Value" << n <<'':''<< endl;
cin>temp;
}
return 0;
}

Note: This Program Will Not Save The Values You Will Enter

it will be updated each time the loop have one pass

e.g.

i will enter 3 for the UserNum and 9,23,7 for the 3 values:

the left hand column will appear on screen and
the right is the tracing

Enter The User Number: 3 UserNum=3
Enter Value 1: 9
UserNum=3 temp= 9
Enter Value 2: 23 UserNum=3
temp= 23
Enter Value 3: 7
UserNum=3 temp= 7

note now after executing the for loop only the last value of temp (
which is 7) could be used further only. the old values (9, then 23)
will be earased as the loop executed

Dec 20 '06 #6

P: n/a
Chidori wrote:
Hi Nicolas:

This is simply the perfect solution for your question

#include<iostream.h>
#include <iostream>
>

int main()
{ int UserNum;
cout << "Enter the User Number:"; // To guide the user to enter the
first value
std::cout << "Enter ...";
cin >UserNum;
std::cin >UserNum;
>
for(int n = 1; n <= UserNum; ++n)
{
int temp;
cout << "Enter Value" << n <<'':''<< endl;
cin>temp;
}
return 0;
}

Note: This Program Will Not Save The Values You Will Enter

it will be updated each time the loop have one pass

e.g.

i will enter 3 for the UserNum and 9,23,7 for the 3 values:

the left hand column will appear on screen and
the right is the tracing

Enter The User Number: 3 UserNum=3
Enter Value 1: 9
UserNum=3 temp= 9
Enter Value 2: 23 UserNum=3
temp= 23
Enter Value 3: 7
UserNum=3 temp= 7

note now after executing the for loop only the last value of temp (
which is 7) could be used further only. the old values (9, then 23)
will be earased as the loop executed
Dec 20 '06 #7

P: n/a
Salt_Peter a écrit :
Chidori wrote:
>Hi Nicolas:

This is simply the perfect solution for your question

#include<iostream.h>

#include <iostream>
>>
int main()
{ int UserNum;
cout << "Enter the User Number:"; // To guide the user to enter the
first value

std::cout << "Enter ...";
> cin >UserNum;

std::cin >UserNum;
if ( std::cin >UserNum )

otherwise:
Enter the User Number: My duck has three legs
Enter the User Number: Enter the User Number: Enter the User Number: ...

'trust no final user' ;)

Dec 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.