473,657 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with reading in a word

7 New Member
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 2638
Banfa
9,065 Recognized Expert Moderator Expert
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
MariyaGel
7 New Member
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 Recognized Expert Moderator Expert
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
1293
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 word; while ( cin >> word ) // ...
7
1775
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: Line 33, column 5: element "NOTE" undefined <note>Last revised: May 29, 2005</note> "You have used the element named above in your document, but the document type you are using does not define an element of that name."
5
6051
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 hits the right and left keys to move this insertion point (cursor) Here is the problem:
14
1585
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
4022
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 on the customized template, reading all the bookmarks and replacing them with desired values, resulting in a new one page document being created which is then saved. This all works fine but when I try to extend the process so that the template...
0
1760
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 .TXT file with Keys/Values
10
1536
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 this : 101,180. é length are 2 in utf-8 file.how can i change this 2 length to 1. my problem that i want to use é like 233 byte and not like 2 bytes 101,180
9
2388
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
16
3737
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 two questions.. 1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1, sizeof(struct bmp), 1, fin); i have a Segment violation ??
24
2052
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 should be discarded and it should ask the user for new input. Ctrl-D should exit from the program. WHAT I GOT: It reads anything beyond 30 characters as the next word :\ and I have to press Ctrl-D two times to exit from the program.
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.