It's my assignment I have done almost I have completed it.But I have problem in getting input from user(the bank name)here is my assignment question.
Customer often wait in the bank..!you have to ask the user the entire as many bank names as he desires and time (waiting time the user waited)
in the following format.
HSBC BANK | 6.4 6.6 7.5 2.2 3.4 5.6 |
Lloyds bank | 5.3 2.3 3.4 5.3 3.4|

the program should calculte the standard deviation and mean.
and the one with least standard deviation should be informed to the user!
The calculation part works perfectly fine,I don't know how to make it in that format!if I use cin.getline it goes to next line after the user inputs the name,so it will change the required format!please help me in getting it in that format!THANKS

#include<iostream>
#include<cmath>
void Mean(double arr[],double size,double &counter,double &mean,double &declared,double sum);
void standardDeviation(double deviation,double &mean,double &counter,double variance,double power,double now,double temp,double arr[],double &declared);
using namespace std;
int main()
{   
    double sum = 0,mean = 0,counter = 0,size = 100,declared = 0 ,variance = 0,now =0,temp =0,power=0,deviation = 0;
	double arr[1000];
    cout<<"enter size"<<endl;
    cin>>declared;
    cout<<"enter input"<<endl;
Mean(arr,size,counter,mean,declared,sum);

standardDeviation(deviation,mean, counter, variance, power, now, temp, arr,declared);
cout<<endl;
system("pause");
}
void Mean(double arr[],double size,double &counter,double &mean,double &declared,double sum)
{
   for(int i=0;i<declared;i++)
    {      
         cin>>arr[i];
          if(arr[i]=='\0')
            break;
             sum+=arr[i];
              counter++;
            }
           // cout<<"counter is: "<<counter;
   //cout<<"sum is : "<<sum<<endl;
   mean=sum/counter;
  counter=counter;
   
     cout<<"mean is: "<<mean<<endl;
   

}

void standardDeviation(double deviation,double &mean,double &declared,double variance,double power,double now,double temp,double arr[],double &counter)
{
   


 for(int i =0; i<counter;i++)
	{
now= arr[i]-mean;
		//cout<<"value: "<<i+1<<"is: "<<now<<"  ";
		power=pow(now,2);
		//cout<<"power of: "<<i+1<<"is: "<<power<<endl;
		temp+=power;
}
	//cout<<"sum of powers are: "<<temp<<endl;
 
  counter=counter-1;
  
 variance=temp/counter;   
//cout<<"Standard Variance"<<variance<<endl;
deviation=sqrt(variance);
cout<<"Standard Deviation is: "<<deviation;




}

Dani AI

Generated

Brief summary and approach. Reading lines with embedded spaces and a variable number of values is easiest by reading whole lines with std::getline, splitting on the pipe |, then parsing the middle token with an std::istringstream into a std::vector<double>. That follows 's and 's hints but fills in the practical details: split the line, trim whitespace, parse the numbers, compute mean and (sample or population) standard deviation, and keep the bank name that yields the smallest stddev.

Example workflow and minimal implementation (read until a blank line):

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <limits>

std::string trim(const std::string &s) {
    size_t l = s.find_first_not_of(" \t");
    if (l == std::string::npos) return "";
    size_t r = s.find_last_not_of(" \t");
    return s.substr(l, r - l + 1);
}

int main() {
    std::string line, best_bank;
    double best_std = std::numeric_limits<double>::max();
    while (std::getline(std::cin, line)) {
        if (line.empty()) break;           // blank line finishes input
        std::istringstream ln(line);
        std::string name, nums;
        if (!std::getline(ln, name, '|') || !std::getline(ln, nums, '|')) continue;
        name = trim(name); nums = trim(nums);
        std::istringstream ns(nums); double v;
        std::vector<double> values;
        while (ns >> v) values.push_back(v);
        if (values.empty()) continue;
        double sum = 0;
        for (size_t i=0;i<values.size();++i) sum += values[i];
        double mean = sum / values.size();
        double ssum = 0;
        for (size_t i=0;i<values.size();++i) { double d = values[i] - mean; ssum += d*d; }
        double variance = (values.size()>1) ? ssum / (values.size()-1) : 0.0; // sample variance
        double stddev = std::sqrt(variance);
        if (stddev < best_std) { best_std = stddev; best_bank = name; }
        std::cout << name << " | mean=" << mean << " stddev=" << stddev << " |\n";
    }
    if (!best_bank.empty()) std::cout << "Least stddev: " << best_bank << " (" << best_std << ")\n";
}

Troubleshooting notes and cautions. In 's original code, arr[i] == '\0' is incorrect (a double should not be compared to '\0'); stream extraction success or vector size should be used instead. Mixing operator>> and std::getline without consuming the leftover newline causes apparent “skipped” lines — either use std::getline everywhere or call cin.ignore() after operator>>. Decide whether to use sample (n-1) or population (n) variance — sample requires at least two samples. Store each bank’s numbers in a vector so the values can be reused for other statistics or debugging. This preserves the required format like HSBC BANK | 6.4 6.6 7.5 2.2 3.4 5.6 | and makes the rest of the calculations straightforward.

Recommended Answers

All 5 Replies

You should simplify the problem. This problem has nothing to do with a bank, so why mention a bank? Ask something like

I want to input a line like
"MyName 2.3 4.5 6.7"
and store the input in a string and 3 doubles. How do I do that?

Dave

I know how to use strings

#include<iostream>
using namespace std;
int main()
{
string name;
double x,y,z;
cout<<"enter name"<<endl;
cin>>name;
cout<<" ";
cin>>x>>y>>x;
return 0;
}

right!
but the problem is If I don't know how many time's the user is going to enter data then how could I use strings for names!
plus the input should be this format
name | 12.2 23.3 343.2 |
secondname | 12.23 34.4 34.4 |
name is the bank name which the user will enter

You should simplify the problem. This problem has nothing to do with a bank, so why mention a bank? Ask something like


Dave

Here's some work for you,



http://www.cprogramming.com/tutorial/lesson3.html

Think that should get you started.

Chris

>>I know how to use strings.
To use STL strings you need to include the header file called string.

To read a string that has whitespace in it, such as the following:

name | 12.2 23.3 343.2 |

you need to use getline(), not >>; as >> stops input into the current variable when it encounters whitespace.

Sooner or later you are going to have to break that line of input up into it's component parts. That's where the advise to look up stringstreams comes in. For example you could use getline() the pipe char as the delimiting char to split the input up into a name string and a data string. You could then use a stringstream to break up the data string into individual numerical data pieces.

As I said in your other thread on this topic, if you don't know the number of lines of input or the number of data points per line, you need a container to hold them if you plan to reuse the information at a later point in the program. If you know an upper limit as to the number of inputs you can use arrays to hold them; just be sure the arrays are large enough to hold everything so you don't overwrite your memory. The alternative is to use a self expanding container so you don't have to worry so much about overwriting memory. Vectors are one container that can do that, but lists and other containers could also be used.

Use the Loops and ask user to enter input. After finishing ask Him something like "Finished" with Y/N? options if Y break the loop and if no Continue. Now I hope you know which loop im talking about :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.