Read from string (possible array usage?) C++ -
I'm trying to write alil function here which basically reads from the string. It reads every three letters and evaluates it using pre-condition (if statement). If the conditions are met then these three letters will be replaced with the new three letters. So this will output new string.
I tried to write the code but the rhetorical argument seems right. The program runs but it does not print anything. Ignore the work name and the inaccuracy I'm just doing a sample function to test this.
string amino_edes (string line) {string acid; String acid; String Newline; For (int i = 0; i & lt; line.length (); i ++) {acid = line [i]; } (Int i = 0; i & lt; 3; i ++) {acid = acid [i]; If (acid == "GUU") {acid = "zap"; } Newline = Acid; } Cout & lt; & Lt; "Acid:" & lt; & Lt; Newline & lt; & Lt; Andal; New line return; }
for (int i = 0; i is "abcd" in the line, this loop is going to do this:
acid = 'A'; Acid = 'b'; Acid = 'c'; Acid = 'd'; Only last assignment has lasting effect. If you really need to bring three letters in the acid from the line - then you could probably add + = to acid instead of adding the letters to = Want to use. But, if you loop on all such lines, then you will make acid = line; I think you acid = line.substr (0, 3) ? for (int i = 0; i & lt; 3; i ++) want to do something else like {acid = acid}; It's going to crash. Acid is definitely a single letter string, and you have acid on 2 and III iterations [1] Code> and Acid [2] . When you are learning C ++, you should probably use .at (i) , when you try to use an invalid index, you can catch exceptions and less Trouble can get some hints at some. As it is, it is undefined behavior. To use it, you need to type the try / hold ... the original form is: < Code> int main ... Try ... ... your code here ... some_string.at (i); } Hold (const std :: exception & e) {std :: cerr & lt; & Lt; "Caught exception:" & lt; & Lt; E.what () & lt; & Lt; '\ N'; } More generally, try entering some std :: cout statement in your code so that you actually know what your variable is worth. You should have easily seen that those who were not expecting you, alternatively, use an interactive debugger and affect the execution of each statement.
Comments
Post a Comment