473,395 Members | 1,613 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.

efficiency problem?

OK, so I am writing a flashcard script for the command prompt (hence, all of my other questions). I had the script working fine, except for the fact that when I displayed a question (or answer) that was more than 1 line long, then it would break in the middle of the word. So I tried to fix this by dividing the question/answer into lines of 73 chars (or less), and then display it line by line.

here is what I came up with:
Expand|Select|Wrap|Line Numbers
  1.     vector<string> q;
  2.     int qsize = ((question.length()/73) + 1);
  3.     switch (qsize)
  4.     {
  5.            case 1:
  6.                 q.push_back(append(question," ", 73));
  7.                 break;
  8.            default:
  9.                 int temp = 73;
  10.                 int temp2 = 0;
  11.                 while (temp < (question.length()+73) )
  12.                 {
  13.                 temp = question.rfind(" ", temp);
  14.                 q.push_back(append(question.substr(temp2,temp)," ", 73));
  15.                 temp2 = temp+1;
  16.                 temp = temp + 73;
  17.                 }
  18.                 break;
  19.     }   
  20.  
Append function:
Expand|Select|Wrap|Line Numbers
  1. //Append function: only works well with append strings of length 1.
  2. string append(string str, string app, int length)
  3. {
  4.     while(str.length()<length)
  5.     {
  6.         str = str + app;
  7.     }
  8.     return str;
  9. }
  10.  
It compiles fine (under Dev-C++), and will display a question with a length of only one line just fine. However, it will not display any questions that are multiple lines. The program just freezes, and sits there. Can anyone tell me why?
Oct 29 '07 #1
5 1386
mac11
256 100+
OK, so I am writing a flashcard script for the command prompt (hence, all of my other questions). I had the script working fine, except for the fact that when I displayed a question (or answer) that was more than 1 line long, then it would break in the middle of the word. So I tried to fix this by dividing the question/answer into lines of 73 chars (or less), and then display it line by line.

here is what I came up with:
Expand|Select|Wrap|Line Numbers
  1.     vector<string> q;
  2.     int qsize = ((question.length()/73) + 1);
  3.     switch (qsize)
  4.     {
  5.            case 1:
  6.                 q.push_back(append(question," ", 73));
  7.                 break;
  8.            default:
  9.                 int temp = 73;
  10.                 int temp2 = 0;
  11.                 while (temp < (question.length()+73) )
  12.                 {
  13.                 temp = question.rfind(" ", temp);
  14.                 q.push_back(append(question.substr(temp2,temp)," ", 73));
  15.                 temp2 = temp+1;
  16.                 temp = temp + 73;
  17.                 }
  18.                 break;
  19.     }   
  20.  
Append function:
Expand|Select|Wrap|Line Numbers
  1. //Append function: only works well with append strings of length 1.
  2. string append(string str, string app, int length)
  3. {
  4.     while(str.length()<length)
  5.     {
  6.         str = str + app;
  7.     }
  8.     return str;
  9. }
  10.  
It compiles fine (under Dev-C++), and will display a question with a length of only one line just fine. However, it will not display any questions that are multiple lines. The program just freezes, and sits there. Can anyone tell me why?
I'll comment on the endless looping...
Your getting stuck in the while loop because rfind keeps giving you the same position (when your looking at the last 73 characters).

Here's an example: assume the line your breaking apart is 90 characters long with spaces at say, 70 and 80 . The first time through you break out the first 70 characters. Then the next time through rfind tells you about the space at position 80, so you break out that stuff. The while condition evaluates to
Expand|Select|Wrap|Line Numbers
  1. while( (80 + 73) < (90 + 73) )
so you go through again (and again), but things don't change - rfind tells you about the space at position 80 again

I won't suggest a solution because I'm sure you can figure that out on your own
Oct 29 '07 #2
THANK YOU SO MUCH!!! I went through My logic soooooo many times but I never found the problem!!
Oct 29 '07 #3
OK, so I was able to fix it, or so I thought, the problem is now that it goes back into the infinite loop if a word wraps around on to two lines normally, such as a word that goes from char 70, to char 80. Here is theh code, any help would be greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1.     vector<string> q;
  2.     int qsize = ((question.length()/73) + 1);
  3.     switch (qsize)
  4.     {
  5.            case 1:
  6.                 q.push_back(append(question," ", 73));
  7.                 break;
  8.            default:
  9.                 int temp = 73;
  10.                 int temp2 = 0;
  11.                 while (temp < (question.length()+73) )
  12.                 {
  13.                 if(question.rfind(" ",temp) == temp)
  14.                 temp = question.length() + 73;
  15.                 else
  16.                 temp = question.rfind(" ", temp);
  17.                 q.push_back(append(question.substr(temp2,temp)," ", 73));
  18.                 temp2 = temp+1;
  19.                 temp = temp + 73;
  20.                 }
  21.                 break;
  22.     }
  23.  
Oct 29 '07 #4
mac11
256 100+
you know how many lines you will have (computed and stored in qsize) and therefore how many times you need to loop - so use qsize to control your loop
Oct 30 '07 #5
that will not work because, say I have a string that is 200 chars long. 200/73 = approx(2.73) + 1 thus qsize is 3 (once typecasted). However, say the string has a space at char 50, and then not again until 74. OK, so that gives you a first line of length 50. Then you go, and the last space in the next line is at char 120. thus, you get a line of 50, and a line of 70. 50 + 70 = 120. It would only loop through 1 more time, and would not get the rest of the string, because 200 - 120 = 80. You see the problem with using the loop from qsize? however, I figured out a different method to do, and it works just fine.
Oct 30 '07 #6

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

Similar topics

24
by: js | last post by:
for a hex conversion, Is it more efficient to do this : Printit is the char whose low order bits represent the nibble to print cout << "0123456789ABCDEF" ; or : const char hexdigits =...
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
92
by: Dave Rudolf | last post by:
Hi all, Normally, I would trust that the ANSI libraries are written to be as efficient as possible, but I have an application in which the majority of the run time is calling the acos(...)...
100
by: jacob navia | last post by:
As everybody knows, C uses a zero delimited unbounded pointer for its representation of strings. This is extremely inefficient because at each query of the length of the string, the computer...
1
by: Tomás | last post by:
dynamic_cast can be used to obtain a pointer or to obtain a reference. If the pointer form fails, then you're left with a null pointer. If the reference form fails, then an exception is thrown....
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
9
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm...
5
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
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: 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: 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
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...

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.