Switch to Linear ModeSwitch to Hybrid ModeSwitch to Threaded Mode
Printer Friendly View | Email this page | Register Now to enjoy user benefits!
SimonTheDriller
SimonTheDriller's Avatar
Registered User
Join Date: Jul 2009
Posts: 21
Trade rep: 0%
C++ programming (Codeblock) SimonTheDriller Aug 16th, 09, 06:28 PM #1 (permalink)
So basically i am coding the program to display 5 temperatures , and the program will be able to display out the readings that the user enter and display out the average temperature as well .

But after writing out the codes , i encounter a tiny problem and i hope some pro can help to enlighten me out .



Basically , i wanna get rid of the last " , "

Quote:
#include <iostream>
using namespace std;

void entry(double temp[]);
void display(double temp[]);
double avg_Temp(double temp[]);

int main()
{
double temp[5];
double average;
cout << "Room Temperature Analysis" << endl;
cout << "=========================" << endl<<endl;
entry(temp); // Call function to allow user to enter the readings
display(temp);
average = avg_Temp(temp);
cout << endl << endl << "Average temperature is : " << average ;


return 0;
}

// Your functions here

void entry(double temp[])
{
int i ;
for (i=0;i<5;i++)
{
cout << "Enter temperature : " ;
cin >> temp[i];
}
return;
}

void display(double temp[])
{
int i ;
cout << endl;
cout << "You have entered the following readings: " ;
for (i=0;i<5;i++)
{
cout << temp[i] << " , " ;
}
return;
}

double avg_Temp(double temp[])
{
int i ;
double sum = 0 , ave ;
for (i=0;i<5;i++)
{
sum = sum + temp[i] ;
}
ave = sum / 5.0 ;
return ave ;
}




Believe in you, not in the me who believes in you; not in the you that believes in me. You should believe... believe in yourself!

ORE GA DARE DA TO OMOTE YARU!!!!!!
 
enriel
enriel's Avatar
Tech Noob
Join Date: Mar 2007
Posts: 251
Trade rep: 100%
enriel Aug 16th, 09, 06:57 PM #2 (permalink)
You can simply edit your for statement in your display function
Quote:
for (i=0;i<5;i++)
{
cout << temp[i] << " , " ;
}
use an if statement with a break function.

It should come out as:
Code:
for (i=0;i<5;i++)
{
    cout <<  temp[i];
    if (i == 4)
       break;
    cout << ",";
}
| Intel 8400 @ 3.60GHz + CM Hyper 212 | ASUS P5Q-E | 2 x 2GB DDR2 Kingston HyperX 1066MHz @ 800MHz | Gigabyte GTS 250 1GB | Seagate Barracuda 320GB 7200.10 | Creative X-Fi XtremeGamer | CM Centurion 5 | Aerocool E85M-700W | Phillips 220CW9FB |

<Razer Diamondback 3G><Razer Mantis Control><Razer Arctosa><Razer Carcharias><Creative Gigaworks T40 Series II>
 
sabe
sabe's Avatar
גם זה יעבור
Join Date: Sep 2004
Location: Serangoon
Posts: 10,283
Trade rep: 100%
sabe Aug 16th, 09, 07:08 PM #3 (permalink)
Code:
int i;
for (i=0;i<5;i++)
{
    cout << temp[i];
    if (i < 4)
        cout << ", ";
}
But you should avoid declaring your i outside if its just used for the loop.

Do this instead:

Code:
for (int i=0; i<5; i++)
{
    cout << temp[i];
    if (i < 4)
        cout << ", ";
}
 
Last edited by sabe; Aug 16th, 09 at 07:10 PM..
SimonTheDriller
SimonTheDriller's Avatar
Registered User
Join Date: Jul 2009
Posts: 21
Trade rep: 0%
SimonTheDriller Aug 16th, 09, 10:40 PM #4 (permalink)
Quote:
Originally Posted by sabe View Post
[code]

But you should avoid declaring your i outside if its just used for the loop.

Do this instead:

Code:
for (int i=0; i<5; i++)
{
    cout << temp[i];
    if (i < 4)
        cout << ", ";
}
but like this the computer will not know whether "i" is a int , double , char wor...

Apart from that , since its a local variable , it would not affect anything outside that function ba .

By the way , Thank You both for your help , , i always encounter problem when it come to displaying out the thingy LOL T.T , guess i still got a long way to go ...
Believe in you, not in the me who believes in you; not in the you that believes in me. You should believe... believe in yourself!

ORE GA DARE DA TO OMOTE YARU!!!!!!
 
universe Registered User
Join Date: Mar 2002
Posts: 2,326
Trade rep: 100%
universe Aug 16th, 09, 11:25 PM #5 (permalink)
Quote:
Originally Posted by SimonTheDriller View Post
but like this the computer will not know whether "i" is a int , double , char wor...

Apart from that , since its a local variable , it would not affect anything outside that function ba .

By the way , Thank You both for your help , , i always encounter problem when it come to displaying out the thingy LOL T.T , guess i still got a long way to go ...
within the for loop, i is a int. the compiler will know

Do not argue with idiots, they will bring you down to their level and beat you with experience.
 
LiM Registered User
Join Date: Apr 2006
Posts: 2,115
Trade rep: 0%
LiM Aug 17th, 09, 12:07 AM #6 (permalink)
for (i=0;i<4;i++)
{
cout << temp[i] << " , " ;
}

cout << temp[5] << " . ";
 
Dreamslacker
Dreamslacker's Avatar
Bipolar Narcissistic
Join Date: Jul 2005
Location: In an idle mind
Posts: 5,954
Trade rep: 100%
Dreamslacker Aug 22nd, 09, 02:09 PM #7 (permalink)
Code:
#include <iostream>
using namespace std;

void entry(double temp[]);
void display(double temp[]);
double avg_Temp(double temp[]);

int main()
{
  double temp[5];
  cout << "Room Temperature Analysis" << endl;
  cout << "=========================" << endl << endl;
  entry(temp); // Call function to allow user to enter the readings
  cout << endl;
  display(temp);
  cout << endl << endl << "Average temperature is : " << avg_Temp(temp);
  return 0;
}

// Your functions here

void entry(double temp[])
{
  int i=0;
  do {
    cout << "Enter temperature : " ;
    cin >> temp[i++];
  } while (i<=5);
  return;
}

void display(double temp[])
{
  int i=0;
  do {
    cout << "You have entered the following readings: " ;
    cout << temp[i++] << ", " ;
  } while (i<5);
  cout << temp[i];
  return;
}

double avg_Temp(double temp[])
{
  double sum = 0;
  for (int i=0;i<5;i++)  sum += temp[i] ;
  return (sum/5.0);
}
Evidently, I'm truely bored..
An Idle Mind Walks in Dark Places

SLS-ing.com - techies on the prowl!
 
New Thread | ↑↓ Similar Threads
Similar Threads Thread Starter Forum Replies Last Post
hubertj Developers and Software Discussion 4 Jan 24th, 09
12:37 AM
dotaman Developers and Software Discussion 9 May 4th, 08
01:10 PM
___nicoleint0x Developers and Software Discussion 13 Apr 25th, 08
08:55 AM
TheHypez Developers and Software Discussion 25 Jan 16th, 08
07:18 PM
Thread Tools Display Modes
Linear Mode Linear Mode
Find the best hotel rates here:
Destination:

City:

Check in Date:


Nights:
Rooms:
Adult(s):
Children:
travel.vr-zone.com
OCZ Fan Club!
OCZ Fan Club 21 OCZ Fans!
Win Visa GiftCard
Win Visa Gift Card