473,396 Members | 1,933 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,396 software developers and data experts.

Array printing problem

109 100+
I want an an array called "message" that I can fit 125 characters into and I want to be able to print that array to a text file.

Here is what I have tried:

Expand|Select|Wrap|Line Numbers
  1. char message[125];
  2. cout<< computera <<": ";
  3. cin>> &message[0], &message[1], &message[2], &message[3], &message[4], &message[5], &message[6], &message[7], &message[8], &message[9], &message[10], &message[11], &message[12], &message[13], &message[14], &message[15], &message[16], &message[17], &message[18], &message[19], &message[20];
  4. FILE *retrieve;
  5. retrieve=fopen("C:\\Program Files\\NetMessage\\retrieve.vbs", "w+" );
  6. fprintf( retrieve, "Const ForWriting = 2\n\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objOutFile = objFSO.OpenTextFile(\"");
  7. fprintf( retrieve, path );
  8. fprintf( retrieve, computerb );
  9. fprintf( retrieve, location );
  10. fprintf( retrieve, "\", ForWriting)\n\nobjOutFile.WriteLine (\"");
  11. fprintf( retrieve, message );
  12. fprintf( retrieve, "\")");
  13. fclose(retrieve);
  14.  
Don't worry about the other bits, I am writing VBScript to a .vbs file, but I need to print "message" in there. It is a mix of C and C++ because I am using header files from both languages for various reasons.

The problem is that it only works if the user enters one word into "message" e.g. Hello, but if the user enters anything with any spaces it doesn't like it e.g. This is a test.
Jun 22 '07 #1
2 1290
archonmagnus
113 100+
I want an an array called "message" that I can fit 125 characters into and I want to be able to print that array to a text file.

Here is what I have tried:

Expand|Select|Wrap|Line Numbers
  1. char message[125];
  2. cout<< computera <<": ";
  3. cin>> &message[0], &message[1], &message[2], &message[3], &message[4], &message[5], &message[6], &message[7], &message[8], &message[9], &message[10], &message[11], &message[12], &message[13], &message[14], &message[15], &message[16], &message[17], &message[18], &message[19], &message[20];
  4.  
  5. (abridged by commenter)
  6.  
...The problem is that it only works if the user enters one word into "message" e.g. Hello, but if the user enters anything with any spaces it doesn't like it e.g. This is a test.
Rather than trying to read-in every character singularly, use the "getline" function of "cin". For example, the code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main (void)
  6. {
  7.     char message[125];
  8.  
  9.     cout<<"Message: ";
  10.     cin.getline(message, 125);
  11.  
  12.     cout<<endl<<"You typed: "<<message<<endl<<endl;
  13.  
  14.     return 0;
  15. }
  16.  
will read and store (up to 125 characters) everything the user enters until the "return" or "enter" key is pressed (whitespace included). Try using getline() and see if that fixes your problem.
Jun 22 '07 #2
niskin
109 100+
Rather than trying to read-in every character singularly, use the "getline" function of "cin". For example, the code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main (void)
  6. {
  7.     char message[125];
  8.  
  9.     cout<<"Message: ";
  10.     cin.getline(message, 125);
  11.  
  12.     cout<<endl<<"You typed: "<<message<<endl<<endl;
  13.  
  14.     return 0;
  15. }
  16.  
will read and store (up to 125 characters) everything the user enters until the "return" or "enter" key is pressed (whitespace included). Try using getline() and see if that fixes your problem.
Yep it has. Thanks for your help.
Jun 22 '07 #3

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

Similar topics

15
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global...
1
by: Ellen Manning | last post by:
The main report shows all medical conferences for a resident by year, by month. A resident can attend many of the same conferences in a year. In the year footer, I have a subreport that summarizes...
3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
3
by: Eric Lilja | last post by:
Assignment: Create a 3x4 2-dimensional array of integers. Populate each element using some non-random scheme so that no two elemens contain the same value. Then create two functions for printing...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
5
by: Jromero | last post by:
I have a problem with printing the array Here is the print out Picture a vehicle number 1 : What's it's max speed?
2
by: serave | last post by:
Hi, i need some help printing a mutlidimensional array, i can do it for square Matriz MxM but i need to print an MXN matrix. here is my code : function printMatrix($matrix){ echo "<table...
3
by: Keith Thompson | last post by:
Victor <vhnguyenn@yahoo.comwrites: You're declaring an array of pointers to unsigned long long, but you're initializing the pointers with integer values. This is actually a constraint...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.