GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Nov-2004, 12:08
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

Nested for loop with function


I have to write a program that calculates the number of times a specific number occurs when the number is generated useing the sum of a series of random number calulations.

I know that the first loop, loops the number of time you want the calculation to occur. The second loop, loops for the number of random numbers you wish to use in the calculation. I don't know if the sum function will corectly add up the random numbers in the second loop.

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

void generate (int, int);

int main()
{
	int a;	// number of time you want to perform this calculation
	int b;	// number of random number used in each calculation
	int c;	// low random value
	int d;	// high random value
	int e;	// number looking for
	int z;	// random number
	int cnt;	// counter
	int sum;	// sum

	cout << "How many times do you want to perform this calculation?" << endl;
	cin >> a;

	cout << "How many random numbers do you want to use in each calculation?" << endl;
	cin >> b;

	cout << "What is the low random value?" << endl;
	cin >> c;

	cout << "What is the high random value?" << endl;
	cin >> d;

	count << "What number are you looking for?" << endl;
	cin >> e;

	
for (i=0;i<a;i++)	//for loop
{
	for (i=0;i<b;i++)	//for loop
{
	sum = generate(c,d);
}
	If (sum = e)
		cin >> cnt = cnt + 1
}

	cout << "The number" << e << "came up" << cnt << "times" << endl;

   return 0;    // indicate the program ended successfully
}

void generate (int c, int d)
{	
	return ( x + rand() % (y + 1 - x));
}

The complier gives out the following errors when I tried to complie:

Quote:
Homework_4-3.cpp: In function `int main()':
Homework_4-3.cpp:43: error: `count' undeclared (first use this function)
Homework_4-3.cpp:43: error: (Each undeclared identifier is reported only once
for each function it appears in.)
Homework_4-3.cpp:47: error: `i' undeclared (first use this function)
Homework_4-3.cpp:51: error: void value not ignored as it ought to be
Homework_4-3.cpp:53: error: `If' undeclared (first use this function)
Homework_4-3.cpp:54: error: syntax error before `>>' token
Homework_4-3.cpp: In function `void generate(int, int)':
Homework_4-3.cpp:64: error: `x' undeclared (first use this function)
Homework_4-3.cpp:64: error: `y' undeclared (first use this function)
Homework_4-3.cpp:64: error: return-statement with a value, in function declared with a void return type
  #2  
Old 05-Nov-2004, 12:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Tori
The complier gives out the following errors when I tried to complie:

Quote:
Homework_4-3.cpp: In function `int main()':
Homework_4-3.cpp:43: error: `count' undeclared (first use this function)
Homework_4-3.cpp:43: error: (Each undeclared identifier is reported only once
for each function it appears in.)
Homework_4-3.cpp:47: error: `i' undeclared (first use this function)
Homework_4-3.cpp:51: error: void value not ignored as it ought to be
Homework_4-3.cpp:53: error: `If' undeclared (first use this function)
Homework_4-3.cpp:54: error: syntax error before `>>' token
Homework_4-3.cpp: In function `void generate(int, int)':
Homework_4-3.cpp:64: error: `x' undeclared (first use this function)
Homework_4-3.cpp:64: error: `y' undeclared (first use this function)
Homework_4-3.cpp:64: error: return-statement with a value, in function declared with a void return type

So: go through the compiler messages one-by-one:

What's wrong with line 43, something to do with "count"? (No, I'm not going to tell you: look at it.)

Fix this line and recompile. You will get something about an undefined symbol 'i' on a certain line (probably line 47). What does that mean? Fix it and recompile.

If there are any specific messages that you don't understand, maybe someone here can help. It's really faster for you (and perhaps even edifying) if you make as many corrections as you can, instead of just throwing things up on the web and hoping that someone will fix things for you.

Regards,

Dave
  #3  
Old 05-Nov-2004, 12:30
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Just to help you with errors.

Homework_4-3.cpp:43: error: `count' undeclared (first use this function)
this should be
CPP / C++ / C Code:
cout << "What number are you looking for?" << endl;

Homework_4-3.cpp:43: error: (Each undeclared identifier is reported only once
for each function it appears in.)

Homework_4-3.cpp:47: error: `i' undeclared (first use this function)

you have not declared variable i, before using it. You can either do it at the begining of main() or in for loop itself (since you are using C++)


Homework_4-3.cpp:51: error: void value not ignored as it ought to be

This error is displayed because your generate function is declared to return nothing (void), where as in definition of function you are returning an integer value. You need to change your function prototype (above main() ) and function definition to change the return type from "void" to "int".

Homework_4-3.cpp:53: error: `If' undeclared (first use this function)

"If" is wrong, "if" is correct syntax.

Homework_4-3.cpp:54: error: syntax error before `>>' token

a semicolan ";" is missing on the line "cin >> cnt = cnt + 1"

Homework_4-3.cpp: In function `void generate(int, int)':

Homework_4-3.cpp:64: error: `x' undeclared (first use this function)
Homework_4-3.cpp:64: error: `y' undeclared (first use this function)

The reason for both of the above error is that. You are using variables "x" and "y" without actually declaring them. Also, the generate function definition has two variable declarations for "c" and "d" but they are not being used anywhere in the generate() function. Is x and y supposed to be c and d respectively ? Just a guess. If yes, then you can straight away replace x and y with c and d respectively , in function.


Homework_4-3.cpp:64: error: return-statement with a value, in function declared with a void return type

This error is again because of of return type missmatch for function generate(). Taking care of error "Homework_4-3.cpp:51:" as mentioned above will eliminate this complier error.
  #4  
Old 05-Nov-2004, 18:02
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

The compiler returned a weird error


I fixed the problems from before, but when I went to compile I got this strange compiler error. I don't understand what it is trying to tell me. (I don't even have a 'operator='.)

Quote:
Homework_4-3.cpp: In function `int main()':
Homework_4-3.cpp:55: error: no match for 'operator=' in '
(&std::cin)->std::basic_istream<_CharT, _Traits>:perator>> [with _CharT =
char, _Traits = std::char_traits<char>]((&cnt)) = (cnt + 1)'
/usr/include/g++/iosfwd:61: error: candidates are: std::basic_istream<char,
std::char_traits<char> >& std::basic_istream<char, std::char_traits<char>
>:perator=(const std::basic_istream<char, std::char_traits<char> >&)

This is my revised code;

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int generate (int, int);

int main()
{
	int a;	// number of time you want to perform this calculation
	int b;	// number of random number used in each calculation
	int c;	// low random value
	int d;	// high random value
	int e;	// number looking for
	int z;	// random number
	int i;	//for control statement in the for loop
	int cnt;	// counter
	int sum;	// sum

	cout << "How many times do you want to perform this calculation?" << endl;
	cin >> a;

	cout << "How many random numbers do you want to use in each calculation?" << endl;
	cin >> b;

	cout << "What is the low random value?" << endl;
	cin >> c;

	cout << "What is the high random value?" << endl;
	cin >> d;

	cout << "What number are you looking for?" << endl;
	cin >> e;

	
for (i=0;i<a;i++)	//for loop
{
	for (i=0;i<b;i++)	//for loop
{
	sum = generate(c,d);
}
	if (sum == e)
		cin >> cnt = cnt + 1;
}

	cout << "The number" << e << "came up" << cnt << "times" << endl;

   return 0;    // indicate the program ended successfully
}

int generate (int c, int d)	// generate function
{	
	return ( c + rand() % (d + 1 - c));
}
  #5  
Old 05-Nov-2004, 18:23
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Quote:
Originally Posted by Tori
cin >> cnt = cnt + 1;

Is this statement valid ? I am not a c++ programmer myself. But what I know about it, it does not look correct to me.

Are you trying to do ?

CPP / C++ / C Code:
cin>> cnt;
cnt=cnt+1;
  #6  
Old 06-Nov-2004, 12:43
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by nkhambal
Is this statement valid ? I am not a c++ programmer myself. But what I know about it, it does not look correct to me.

Are you trying to do ?

CPP / C++ / C Code:
cin>> cnt;
cnt=cnt+1;
You got it. The error message is telling you there is no '=' operator for the class you are using, which is cin.
__________________

Age is unimportant -- except in cheese
  #7  
Old 08-Nov-2004, 09:53
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

It will compile, but won't run all the way


I fixed the cnt error and now it will compile. When I run the program, it will only run up to the point where I enter the number that I am looking for. Why is it stopping and not performing the loops?
  #8  
Old 08-Nov-2004, 10:25
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Pls post your new code and the output.

Thanks,
  #9  
Old 08-Nov-2004, 10:54
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

Here is the updated code


CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int generate (int, int);

int main()
{
	int a;	// number of time you want to perform this calculation
	int b;	// number of random number used in each calculation
	int c;	// low random value
	int d;	// high random value
	int e;	// number looking for
	int z;	// random number
	int i;	//for control statement in the for loop
	int cnt;	// counter
	int sum;	// sum

	cout << "How many times do you want to perform this calculation?" << endl;
	cin >> a;

	cout << "How many random numbers do you want to use in each calculation?" << endl;
	cin >> b;

	cout << "What is the low random value?" << endl;
	cin >> c;

	cout << "What is the high random value?" << endl;
	cin >> d;

	cout << "What number are you looking for?" << endl;
	cin >> e;

	
for (i=0;i<a;i++)	//for loop
{
	for (i=0;i<b;i++)	//for loop
{
	sum = generate(c,d);
}
	if (sum == e)
{		
		cnt = cnt +1;
		cin >> cnt;
}
}

	cout << "The number" << e << "came up" << cnt << "times" << endl;

   return 0;    // indicate the program ended successfully
}

int generate (int c, int d)	// generate function
{	
	return ( c + rand() % (d + 1 - c));
}
  #10  
Old 08-Nov-2004, 11:12
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by Tori
CPP / C++ / C Code:
// code snipped

	int i;	//for control statement in the for loop

// code snipped
	
for (i=0;i<a;i++)	//for loop using "i" to control loop
{
    for (i=0;i<b;i++)	//for loop  using "i" to control loop
    {
        sum = generate(c,d);
    }
    if (sum == e)
    {		
        cnt = cnt +1;
        cin >> cnt;  // why are you asking for a cnt val to be inputted?
                        // did you want cout << to see the count increment?
    }
}

I bet you can fix it now. :-)
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
Last edited by cable_guy_67 : 08-Nov-2004 at 11:15. Reason: commented cin in loop
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to interpret characters as they are being entered? nkhambal C Programming Language 18 14-Feb-2006 10:41
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 19:46
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 08:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.