473,395 Members | 2,079 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,395 software developers and data experts.

string arrays

hey, i have only been working with c for a very short time and i have a queston that seems to be poorly covered in all areas i have looked.

i am attempting to create an array of strings so that 9 strings start out as "----------" then are replaced by user input with a for loop. Knowing that strings are just character arrays, i initially tried just creating a 2d character array but it became quickly apparent that this was not the best way to go about it. after some research, i have been trying to use pointers, but the code is still not working properly. relevant code is as follows:

Expand|Select|Wrap|Line Numbers
  1. char *batter[9] = {
  2.     "----------",
  3.     "----------",
  4.     "----------",
  5.     "----------",
  6.     "----------",
  7.     "----------",
  8.     "----------",
  9.     "----------",
  10.     "----------"
  11. };
  12. int main(int argc,char *argv[])
  13. {
  14.      for(l = 0; l < 9; l++) {
  15.           printf("\n\nWhat is the name of batter %d?",l+1);
  16.           gets(batter[l]);
  17.           clr();
  18.           for(k = 0; k < 9; k++) {
  19.     printf("%s \n",batter[k]);
  20.           }
  21.      }
  22.      return 0;
  23. }
unfortunetly, this every time the user enters a string, it changes all strings to that value instead of only the intended one. a steer in the correct direction would be greatly appreaciated.
Thanks.
Mar 8 '07 #1
4 1804
Ganon11
3,652 Expert 2GB
Instead of using nested for...loops, you should use 2 separate loops - the first should fill the array (ending after the gets call), and the second should display (starting after the clr() call).
Mar 8 '07 #2
that's not really what i'm trying to do, if possible i'd like to print them all after every entry.

regardless, i tried it anyway and it still just prints them all as the last entry. and other suggestions on how to make it work either way would be great.
Mar 9 '07 #3
horace1
1,510 Expert 1GB
in this definition
Expand|Select|Wrap|Line Numbers
  1. char *batter[9] = {
  2.     "----------",
  3.     "----------",
  4.     "----------",
  5.     "----------",
  6.     "----------",
  7.     "----------",
  8.     "----------",
  9.     "----------",
  10.     "----------"
  11. };
  12.  
the strings "----------" are constants and attempting to change them will give a segmentation error.
Allocate a 2D array initialised with the strings, e.g.
Expand|Select|Wrap|Line Numbers
  1. char batter[9][12] = {
  2.     "----------",
  3.     "----------",
  4.     "----------",
  5.     "----------",
  6.     "----------",
  7.     "----------",
  8.     "----------",
  9.     "----------",
  10.     "----------"
  11. };
  12. int main(int argc,char *argv[])
  13. {
  14.      int l,k;                             // added
  15.      for(l = 0; l < 9; l++) {
  16.           printf("\n\nWhat is the name of batter %d?",l+1);
  17.           gets(batter[l]);
  18.           //clr();
  19.           for(k = 0; k < 9; k++) {
  20.     printf("%s \n",batter[k]);
  21.           }
  22.      }
  23.      return 0;
  24. }
  25.  
  26.  
you should check that the string read by fgets() does not exceed the storage in the array
Mar 9 '07 #4
thank you, that did it, appreaciate the all the help
Mar 9 '07 #5

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

Similar topics

3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
18
by: greenflame | last post by:
I want to make a function that does the following. I will call it thefunc for short. '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>"
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
3
by: shapper | last post by:
Hello, I have two arrays of strings, s1 and s2: s1 = "london, lisbon, paris, newyork" s2 = "lisbon, yellow, France" s2 will have only one item included in s1.
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
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.