473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with reading in a word

I have wrote the program and it worked fine until I had to include one more array into it. As you can see below the two arrays of volume and mass are working properly now I need to include a third array which reads in the sample name that coincides with the mass and the volume. I have tried the getline function and that didnt work. This is my first class in computer programming and im very new to it so if i have made obviouse mistakes i apologize. I tried as much as i can myself and Im not exactly sure how to proceed. Please help

Here is the program:
Expand|Select|Wrap|Line Numbers
  1. # include <string>
  2. # include <iostream>
  3. # include <fstream>
  4. using namespace std;
  5. void readinput (double[], double[], string, int, ifstream &infile);
  6. void printvalues (double[], double[], string, int, ofstream &outfile);
  7. void linearsort (double[], double[], string, int);
  8. void bubblesort (double[], double[], string, int);
  9. int line_search (double[], double, int);
  10. int main()
  11. {
  12.     ofstream outfile ("prog7.txt");
  13.     ifstream infile ("assignment7.txt");
  14.     int n, position;
  15.     if (infile.bad())
  16.     {
  17.        cout<<"unable to read from file";
  18.        exit(1);
  19.     }
  20.     infile>>n;
  21.     double mass[n], volume[n], newnumber; 
  22.     string name;  
  23.     readinput(mass, volume, name, n, infile);
  24.     cout<<name[0]<<name[1]<<name[2]<<name[3]<<name[4]<<name[5];
  25.     printvalues(mass, volume, name, n, outfile);
  26.     linearsort(mass, volume, name, n);
  27.     printvalues(mass, volume, name, n, outfile);
  28.     bubblesort(volume, mass, name, n);
  29.     printvalues(mass, volume, name, n, outfile);     
  30.     while(infile>>newnumber)
  31.     {
  32.           position=line_search(mass, newnumber, n);
  33.           if (position == -1)
  34.           {
  35.               cout<<"The number "<<newnumber
  36.                   <<" does not occure in the mass array"<<endl;  
  37.           }
  38.           else
  39.           {
  40.               cout<<newnumber<<" is found at position "<<position
  41.                   <<". The volume of that sample is "<<volume[position]<<endl;
  42.           }
  43.           position=line_search(volume, newnumber, n);
  44.           if (position == -1)
  45.           {
  46.               cout<<"The number "<<newnumber
  47.                   <<" does not occure in the volume array"<<endl;
  48.           }
  49.           else
  50.           {
  51.               cout<<newnumber<<" is found at position "<<position
  52.                   <<". The mass of that sample is "<<mass[position]<<endl;
  53.           }
  54.           cout<<endl;
  55.     }       
  56.     infile.close();
  57.     outfile.close();
  58.     getchar();
  59.     return 0; 
  60. }
  61.  
  62.  
  63. //Function readinput:
  64. //Input: 2 arrays, int n, and infile
  65. //Process: Read the data values from the input values into 2 arrays.
  66. void readinput (double a[], double b[], string c[], int n, ifstream &infile)
  67. {
  68.      for (int i=0; i<n; i++)
  69.           infile>>a[i]>>b[i]>>c[i];
  70.      return;
  71. }//read input
  72.  
  73.  
  74.  
  75. //Function bubblesort:
  76. //Input: 2 arrays
  77. //Process: Sorts the arrays in ascending order of volume
  78. void bubblesort (double a[], double b[], string c[], int n)
  79. {
  80.        double temp, temp2;
  81.        string temp3;
  82.        bool swapped;
  83.        do
  84.        {
  85.             swapped = false;
  86.             for (int i=0; i<n-1; i++)
  87.                  if (a[i] > a[i+1])
  88.                  {
  89.                     temp = a[i];
  90.                     temp2 = b[i];
  91.                     temp3 = c[i];
  92.                     a[i] = a[i+1];
  93.                     b[i] = b[i+1];
  94.                     c[i] = c[i+1];
  95.                     a[i+1] = temp;
  96.                     b[i+1] = temp2;
  97.                     c[i+1] = temp3;
  98.                     swapped = true;
  99.                  }
  100.        }
  101.        while (swapped);
  102.        return;
  103. }//bubblesort
  104.  
  105.  
  106. //Function linearsort:
  107. //Input: 2 arrays
  108. //Process: Sorts the arrays in descending order of mass
  109. void linearsort (double a[], double b[], string c[], int n)
  110. {
  111.      double temp, temp2;
  112.      string temp3;
  113.      for (int i=0; i<n-1; i++)
  114.           for (int j=i+1; j<n; j++)
  115.                if (a[i] < a[j])
  116.                {
  117.                   temp = a[i];
  118.                   temp2 = b[i];
  119.                   temp3 = c[i];
  120.                   a[i] = a[j];
  121.                   b[i] = b[j];
  122.                   c[i] = c[j];
  123.                   a[j] = temp;
  124.                   b[j] = temp2;
  125.                   c[j] = temp3;
  126.                }
  127.      return;
  128. }// linearsort
  129.  
  130.  
  131. //Function linsearch:
  132. //Input: double array, number to be searched for and n
  133. //Process: Determines whether or not the input number occurs in the array
  134. //Output: The position of that number or -1 if it cannot be found
  135. int line_search (double a[], double newnumber, int n)
  136. {
  137.     for (int position=0; position<n; position++)
  138.          if (a[position] == newnumber)
  139.              return position;
  140.     return -1;
  141. }//line_search
  142.  
  143.  
  144. //Function printvalues:
  145. //Input: 2 arrays, int n and outfile
  146. //Process: To print the arrays into a table.
  147. void printvalues (double a[], double b[], string c[], int n, ofstream &outfile)
  148. {
  149.      outfile<<"        Collected Data"<<endl;
  150.      outfile<<"Mineral      Mass      Volume"<<endl;
  151.      for (int i=0; i<n; i++)
  152.      {
  153.           outfile.setf(ios_base::fixed, ios_base::floatfield);
  154.           outfile.precision(2);
  155.           outfile.setf(ios::left);
  156.           outfile.width(12);
  157.           outfile<<c[i]<<a[i]<<b[i]<<endl;
  158.      }      
  159.      outfile<<endl<<endl;
  160.      return;
  161. }//printvalues
Nov 29 '06 #1
3 2627
Banfa
9,065 Expert Mod 8TB
I think you problem is that using you syntax

string name;

should be

string name[n];

In fact you have notably left out the array specifiers [] in a lot of places where you are passing the name parameter.

Additionally the code

double mass[n];

doesn't compile for me as standard C++ does not support dynamically sized arrays (not sure which version you are using though).
Nov 30 '06 #2
That was absolutely correct I can't believe I missed that. It worked out perfectly , Im using Dev-C++ and double mass[n] workes with no problem. I just got confused how i was suppose to declare an array of words, I wasnt sure you can have an array or words and not just characters.
Thank you very much
Nov 30 '06 #3
Banfa
9,065 Expert Mod 8TB
Im using Dev-C++ and double mass[n] workes with no problem.
That is fine but you had better be sure that your tutor/lecturer is too (assuming you have one.

...

Now on a point of style you have code similar to the following

Expand|Select|Wrap|Line Numbers
  1.     temp = a[i];
  2.     temp2 = b[i];
  3.     temp3 = c[i];
  4.     a[i] = a[i+1];
  5.     b[i] = b[i+1];
  6.     c[i] = c[i+1];
  7.     a[i+1] = temp;
  8.     b[i+1] = temp2;
  9.     c[i+1] = temp3;
  10.  
In both of your sort routines. Now this code works (which is why it is a style point and not a logic point) but this is not very elegant, this code logical performs

For array a swop the values located at i and i+1
For array b swop the values located at i and i+1
For array c swop the values located at i and i+1

which you have written long handed. To neaten it you could easily write Swop functions for the 2 types of variable use (double and string) something with the prototypes

void Swop(double &op1, double &op2);
void Swop(string &op1, string &op2);

these functions would probably embody the code you already have (i.e. use a temporary variable) and then the code becomes

Expand|Select|Wrap|Line Numbers
  1.     Swop(a[i], a[i+1]);
  2.     Swop(b[i], b[i+1]);
  3.     Swop(c[i], c[i+1]);
  4.  
which looks much better and says what is happening.

If you really wanted to get snazzy rather than write 2 functions you could write a single template function

template <class T>
void Swop(T &op1, T &op2);

but that may be a little bit complex for a beginner.
Nov 30 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ???? | last post by:
this is an excerpt from C++ Primer 3rd edition by Lippman, Lajoie. How might we read an unknown number of input values? At the end of Section 1.2, we did just that. The code sequence string...
7
by: Steven (remove wax and invalid for reply) | last post by:
I'm moving an old page to a new host, and trying to modernize it at the same time. There are two little problems that are driving me nuts. 1. Validation of www.marzolian.com/index.html says: ...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
14
by: placid | last post by:
Hi all, i have this struct for a binary tree node typedef struct treenode { char *word; struct treenode *right; struct treenode *left; }TreeNode;
0
by: Raj Singh | last post by:
I am facing a problem with Visual C# 2003 and MS Word XP. I am using a customized template for Word and in that template I have defined some bookmarks. I am trying to create a Word document based...
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
10
by: tvin | last post by:
Hi all I brought a string from a .txt file which was saved like utf-8. In the .txt file i have this string "frédéric".My problem is that when i read this file .txt,the bytes of é are like...
9
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
24
by: arnuld | last post by:
I have a function named getword that read every single input from std. input. WHAT I WANTED: I want it read the word if it has less than or equal to 30 characters. Anything else beyond that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.