| | |
strings, arrays, length program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>>Ok. I deleted line 32, but by doing that the program does not allow me to actually enter a string
Yes it does -- that is what the getline() function does. getline() accepts all that characters you type up to the <Enter> key (newline character). You can alter that behavor, but for now just use the default.
>>Still I need to know how to use to .length() function and where it comes into play
Don't worry about that just yet. Get this part of the program working first. After you get it to compile AND WORK correctly, then go back and re-read the program requirements that you originally posted
Yes it does -- that is what the getline() function does. getline() accepts all that characters you type up to the <Enter> key (newline character). You can alter that behavor, but for now just use the default.
>>Still I need to know how to use to .length() function and where it comes into play
Don't worry about that just yet. Get this part of the program working first. After you get it to compile AND WORK correctly, then go back and re-read the program requirements that you originally posted
•
•
•
•
Use length function to find the length of each string. Store lengths in int array.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
Alright... think I got it, now. Seems to run correctly. See what you think or if you might have any suggestions. Thanks.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> // For setw() and setprecision() #include <string> // For using strings within program using namespace std; // Define const int MAX_SIZE = 10; // Size of Arrays int main() { // Variable Declarations string A[MAX_SIZE]; // Array of strings int I[MAX_SIZE]; // Array of integers int n = 0; // Number of strings read int i = 0; // For loops, element int lengthofAll = 0; // Length of all strings input by user float averageLength = 0.0; // Average length of strings string longestWord; // Longest word input by user int lengthofLongest = 0; // Length of longest string input by user int colWidth = 0; // Width of columns (lengthofLongest + 3) // Output - prompt user to enter number of strings cout << "Enter the number of strings (1-10):"; cin >> n; // Verify that 1<= n <= 10 if ( (n<1) | (n>10) ) { cout << "Enter a new number (1-10):" << endl; cin >> n; return -1; } // Loop - prompt user to input strings for (i=0; i < n; i++) { cout << "Enter strings:" << endl; getline(cin, A[i]); // use getline function to read whole line of strings cin >> A[i]; I[i] = A[i].length(); // use length function to calculate length lengthofAll = lengthofAll + I[i]; if (lengthofLongest < I[i]) //Calculations { lengthofLongest = I[i]; longestWord = A[i]; colWidth = lengthofLongest + 3; } } // Table Headings cout << endl; cout << setw(colWidth) << "Words" << setw(colWidth) << "Length" << endl; // Table Body Setup for (i=0; i<n; i++) { cout << fixed << setw(colWidth) << A[i] << setw(colWidth) << I[i] << endl; } // Data Calculated from user's input cout << endl; cout << "Strings Processed:" << n << endl; cout << "Longest String:" << longestWord << endl; cout << "Length of Longest:" << lengthofLongest << endl; averageLength = (float) lengthofAll / n; // Calculation for averageLength cout << "Average Length:" << setprecision(2) << averageLength << endl; // End program and return control to the operating system return 0; }
Last edited by curls; Jan 25th, 2008 at 2:43 pm.
![]() |
Similar Threads
- QBASIC and Visual Basic (Legacy and Other Languages)
- Need help with strings in C programming (C)
- problem with phonebook program (C)
- palindrome program (Legacy and Other Languages)
- Is there a way to tokenize an array of strings (C++)
- How to improve program with string compairing? (C++)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- how will i repeat my whole game program? (Java)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help on a School Project
- Next Thread: arrays, addition, and fibonacci
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






