Displaying my output

The number entered in my running total does not display & "ad

Page 1 of 1

11 Replies - 801 Views - Last Post: 01 December 2008 - 02:27 AM Rate Topic: -----

#1 cball1321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 08

Displaying my output

Post icon  Posted 30 November 2008 - 08:50 PM

Quote

The second number entered in my running total does not display . The program just doubles it in the running total & "add" is not displayed after every odd larger number in the running total.

# include <iostream> // for input/ output
using namespace std;

int numbers (string str)
{	
   int num;
   cout << str; 
   cin >> num;
   
	
   while ( num <= 0 )
   {  
	   cout << "Error !! Re-enter a positive, non-zero number. ";
	   cin >> num;
	   cout << endl;
   }
	
	return (num);
	
}

int main ()
{
	// variables
	int num1, num2, num;
	int smaller;
	int larger;
	int result;
	string str1 = "Enter the first number to be multiplied. ";
	string str2 = "Enter the second number to be multiplied. ";
	string odd = "add";
	
	cout << "This program will multiply two numbers. " << endl << endl;
	
	num1 = numbers ( str1 ); // 1st call 
	
	num2 = numbers ( str2 ); // 2nd call
	
	if ( num1 > num2 )
	{
	   larger = num1;
	   smaller = num2;
	}
	else
	{
	   larger = num2;
	   smaller = num1;
	}
	
   
	for (larger; larger >= 1; larger /= 2 )
	{  
	   if (smaller != larger)
		  smaller *= 2;  
	   
	   if ( larger % 2 > 0 )
		  odd = "add";
		  
		  cout << larger  << "	" << smaller << "   " << odd << endl;  
	}  
	
	cout << endl;
	
	result = num1 * num2;
	
	cout << num1 << " x " << num2 << " = " << result;   
   
	cout << endl << endl;
	system ("pause");
	return 0;
}


This post has been edited by cball1321: 30 November 2008 - 09:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying my output

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Displaying my output

Posted 30 November 2008 - 09:50 PM

1 - You don't us the 'quote' tags for your own text. Quote is for when you are quoting someone else. Your text just goes in the open box like this is.


2 - Have a look at the lines that the compiler is warning you about:

dic.cpp: In function ‘int main()’:
dic.cpp:51: warning: statement has no effect
dic.cpp:25: warning: unused variable ‘num’

Do you see problems on lines 25:
int num1, num2, num;
and line 51:
for (larger; larger >= 1; larger /= 2 )
that you can work on?

We have had the discussion about the problem on line 51 a few times now. Have a look back at the earlier posting and/or check your textbook for how to write a for loop.
Was This Post Helpful? 0
  • +
  • -

#3 cball1321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 08

Re: Displaying my output

Posted 30 November 2008 - 09:56 PM

View Postjanotte, on 30 Nov, 2008 - 08:50 PM, said:

1 - You don't us the 'quote' tags for your own text. Quote is for when you are quoting someone else. Your text just goes in the open box like this is.


2 - Have a look at the lines that the compiler is warning you about:

dic.cpp: In function ‘int main()’:
dic.cpp:51: warning: statement has no effect
dic.cpp:25: warning: unused variable ‘num’

Do you see problems on lines 25:
int num1, num2, num;
and line 51:
for (larger; larger >= 1; larger /= 2 )
that you can work on?

We have had the discussion about the problem on line 51 a few times now. Have a look back at the earlier posting and/or check your textbook for how to write a for loop.



The loop works just fine. I need help with displaying add after every odd "larger" number.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Displaying my output

Posted 30 November 2008 - 10:27 PM

View Postcball1321, on 30 Nov, 2008 - 08:56 PM, said:

The loop works just fine. I need help with displaying add after every odd "larger" number.


Humour me and write some code that will compile without any errors nor any warnings and then we'll look at the logic of your code.
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Displaying my output

Posted 30 November 2008 - 10:34 PM

View Postcball1321, on 30 Nov, 2008 - 07:50 PM, said:

Quote

The second number entered in my running total does not display . The program just doubles it in the running total & "add" is not displayed after every odd larger number in the running total.

# include <iostream> // for input/ output
using namespace std;

int numbers (string str)
{	
   int num;
   cout << str; 
   cin >> num;
	
   while ( num <= 0 )
   {  
	   cout << "Error !! Re-enter a positive, non-zero number. ";
	   cin >> num;
	   cout << endl;
   }

   return (num);
}

int main ()
{
	// variables
	int num1, num2, num;
	int smaller;
	int larger;
	int result;
	string str1 = "Enter the first number to be multiplied. ";
	string str2 = "Enter the second number to be multiplied. ";
	string odd = "add";
	
	cout << "This program will multiply two numbers. " << endl << endl;
	
	num1 = numbers ( str1 ); // 1st call 
	num2 = numbers ( str2 ); // 2nd call
	
	if ( num1 > num2 )
	{
	   larger = num1;
	   smaller = num2;
	}
	else
	{
	   larger = num2;
	   smaller = num1;
	}
   
	for (larger; larger >= 1; larger /= 2 )
	{  
	   if (smaller != larger)
		  smaller *= 2;  
	   
	   if ( larger % 2 > 0 )
		  odd = "add";
		  
           cout << larger  << "	" << smaller << "   " << odd << endl;  
	}  
	
	cout << endl;
	
	result = num1 * num2;
	
	cout << num1 << " x " << num2 << " = " << result;   
   
	cout << endl << endl;

	system ("pause");
	return 0;
}


The odd string is always printed. It's value is always "add".
...
string str1 = "Enter the first number to be multiplied. ";
string str2 = "Enter the second number to be multiplied. ";
//string odd = "add";
string odd;
...
if ( larger % 2 > 0 )
   odd = "add";
else
   odd.clear();
...


Quote

This program will multiply two numbers.

Enter the first number to be multiplied. 25
Enter the second number to be multiplied. 26
26 50
13 100 add
6 200
3 400 add
1 800 add

25 x 26 = 650

Press any key to continue . . .

Was This Post Helpful? 1
  • +
  • -

#6 cball1321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 08

Re: Displaying my output

Posted 30 November 2008 - 10:41 PM

View Postjanotte, on 30 Nov, 2008 - 09:27 PM, said:

View Postcball1321, on 30 Nov, 2008 - 08:56 PM, said:

The loop works just fine. I need help with displaying add after every odd "larger" number.


Humour me and write some code that will compile without any errors nor any warnings and then we'll look at the logic of your code.

I know that you are an expert at this but I'm telling you the loop compiles with no errors and it executes. I'm a beginners barely in my second CS course. I don't know why it works it just does.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying my output

Posted 30 November 2008 - 10:46 PM

Oddly enough that was my drive to do computer science in the first place. I always want to know how it works.
Was This Post Helpful? 0
  • +
  • -

#8 cball1321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 08

Re: Displaying my output

Posted 30 November 2008 - 10:50 PM

[quote name='n8wxs' date='30 Nov, 2008 - 09:34 PM' post='477471']

View Postcball1321, on 30 Nov, 2008 - 07:50 PM, said:

Quote

The second number entered in my running total does not display . The program just doubles it in the running total & "add" is not displayed after every odd larger number in the running total.

# include <iostream> // for input/ output
using namespace std;

int numbers (string str)
{	
   int num;
   cout << str; 
   cin >> num;
	
   while ( num <= 0 )
   {  
	   cout << "Error !! Re-enter a positive, non-zero number. ";
	   cin >> num;
	   cout << endl;
   }

   return (num);
}

int main ()
{
	// variables
	int num1, num2, num;
	int smaller;
	int larger;
	int result;
	string str1 = "Enter the first number to be multiplied. ";
	string str2 = "Enter the second number to be multiplied. ";
	string odd = "add";
	
	cout << "This program will multiply two numbers. " << endl << endl;
	
	num1 = numbers ( str1 ); // 1st call 
	num2 = numbers ( str2 ); // 2nd call
	
	if ( num1 > num2 )
	{
	   larger = num1;
	   smaller = num2;
	}
	else
	{
	   larger = num2;
	   smaller = num1;
	}
   
	for (larger; larger >= 1; larger /= 2 )
	{  
	   if (smaller != larger)
		  smaller *= 2;  
	   
	   if ( larger % 2 > 0 )
		  odd = "add";
		  
           cout << larger  << "	" << smaller << "   " << odd << endl;  
	}  
	
	cout << endl;
	
	result = num1 * num2;
	
	cout << num1 << " x " << num2 << " = " << result;   
   
	cout << endl << endl;

	system ("pause");
	return 0;
}


The odd string is always printed. It's value is always "add".
...
string str1 = "Enter the first number to be multiplied. ";
string str2 = "Enter the second number to be multiplied. ";
//string odd = "add";
string odd;
...
if ( larger % 2 > 0 )
   odd = "add";
else
   odd.clear();
...


Quote

This program will multiply two numbers.

Enter the first number to be multiplied. 25
Enter the second number to be multiplied. 26
26 50
13 100 add
6 200
3 400 add
1 800 add

25 x 26 = 650

Press any key to continue . . .
I thought we had it but it still places add after some of the even larger numbers.[quote]


View PostKYA, on 30 Nov, 2008 - 09:46 PM, said:

Oddly enough that was my drive to do computer science in the first place. I always want to know how it works.

I've try many different configurations for the the initialization statement in the for loop and they all work, so I'm leaving the loop the way it is. If I'm wrong, I'm wrong.[quote]
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying my output

Posted 30 November 2008 - 10:53 PM

Never said you were wrong. Is there still a question to be answered? I can't decipher your last post.
Was This Post Helpful? 0
  • +
  • -

#10 cball1321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 08

Re: Displaying my output

Posted 30 November 2008 - 11:16 PM

View PostKYA, on 30 Nov, 2008 - 09:53 PM, said:

Never said you were wrong. Is there still a question to be answered? I can't decipher your last post.

I have to display "add" after every odd larger number. The program is not doing this.[quote]
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Displaying my output

Posted 30 November 2008 - 11:19 PM

if ( larger % 2 > 0 )
   cout << "add";


Was This Post Helpful? 1
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Displaying my output

Posted 01 December 2008 - 02:27 AM

View Postcball1321, on 30 Nov, 2008 - 09:41 PM, said:

I know that you are an expert at this but I'm telling you the loop compiles with no errors and it executes. I'm a beginners barely in my second CS course. I don't know why it works it just does.


I'm very far from an expert but thank you for the vote of confidence (no matter how misplaced).

Yes I agree that the compiler is not producing an error. It is producing a warning that you have coded something on line 51 that isn't doing anything useful.

Since we don't usually code things to feel our fingers press the keys we can safely assume that you intended that everything you coded on that line was intended to do something useful. Something isn't passing that test.

The compiler warnings are worth heeding because they can reveal problems in the syntax or layout in code that will compile but not run correctly.

This may or may not be the case here.

I hope it isn't the case and that the assistance from others has resolved all your problems. KYA and n8wxs are far ahead of me in every respect and the quality of their advice surpasses mine.

If things are still not working properly though you may want to revisit line 51 and see if you can get rid of that warning.

I don't know tools you are using but in many tools you have to ask for the compiler to show you warnings as well as errors and my advice to everyone (learners and otherwise) is to allow the compiler to help you by showing you all the warnings. I'd suggest a hunt around in the options and/or documentation to see if you can work out how to turn on display of all warnings even if you now have this particular problem sorted. It will pay you back in hours of debugging avoided in the long run.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1