Can You Shift an Array by a User-Inputted Value Using For Loops?

  • Thread starter Thread starter AST3019
  • Start date Start date
  • Tags Tags
    Array Indices
Click For Summary
SUMMARY

The discussion focuses on using for loops in C++ to shift an array based on a user-inputted value. The original code contained a logical error in the for loop condition, using "s == b" instead of "s < b", which prevented the loop from executing correctly. After identifying and correcting this mistake, the user successfully achieved the desired functionality of shifting the array elements. This highlights the importance of proper loop conditions in programming.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with arrays and indexing in C++
  • Basic knowledge of user input handling in C++
  • Experience with control flow statements, particularly for loops
NEXT STEPS
  • Study C++ array manipulation techniques
  • Learn about C++ control flow and loop structures
  • Explore error handling and debugging in C++
  • Investigate user input validation methods in C++
USEFUL FOR

Beginner to intermediate C++ programmers, educators teaching programming concepts, and anyone interested in improving their understanding of array manipulation and loop logic in C++.

AST3019
Messages
4
Reaction score
0
So I am trying to use for loops to shift the index an array by a user inputted value. The code i have so far is
printMessage();
cin >> choice;
if(choice == 2){
count << endl;
count <<"Please enter the desired number of shifts:" << endl;
cin >> b;
int s;
for (s = 0; s == b;s++) {
count << message[s + b];
}

}
When I try to compile it, it doesn' count the message. The input from the message occurs earlier in the code.
 
Technology news on Phys.org
Have a think about why you put s == b in the for loop. It doesn't make any sense at all.
 
yeah, right after i posted the thread i realized that and fixed it and now it works. thanks though.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
12
Views
2K
  • · Replies 14 ·
Replies
14
Views
35K
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 4 ·
Replies
4
Views
6K